All Projects → XiangpengHao → cxx-cmake-example

XiangpengHao / cxx-cmake-example

Licence: other
An example repo to setup cxx (Rust FFI library) with the CMake build system.

Programming Languages

rust
11053 projects
C++
36643 projects - #6 most used programming language
CMake
9771 projects
Dockerfile
14818 projects

Labels

Projects that are alternatives of or similar to cxx-cmake-example

Lzmq
Lua binding to ZeroMQ
Stars: ✭ 110 (+71.88%)
Mutual labels:  ffi
Rust Bindgen
Automatically generates Rust FFI bindings to C (and some C++) libraries.
Stars: ✭ 2,453 (+3732.81%)
Mutual labels:  ffi
Awesome Php Ffi
PHP FFI examples and use cases
Stars: ✭ 217 (+239.06%)
Mutual labels:  ffi
Swift Haskell Tutorial
Integrating Haskell with Swift Mac Apps
Stars: ✭ 118 (+84.38%)
Mutual labels:  ffi
Rustr
Rust and R Integration
Stars: ✭ 160 (+150%)
Mutual labels:  ffi
Emacs Module Rs
Rust binding and tools for emacs-module (Emacs's dynamic module support)
Stars: ✭ 203 (+217.19%)
Mutual labels:  ffi
Android Luajit Launcher
Android NativeActivity based launcher for LuaJIT, implementing the main loop within Lua land via FFI
Stars: ✭ 87 (+35.94%)
Mutual labels:  ffi
Rustler
Safe Rust bridge for creating Erlang NIF functions
Stars: ✭ 3,052 (+4668.75%)
Mutual labels:  ffi
Goluwa
a game framework written in luajit
Stars: ✭ 173 (+170.31%)
Mutual labels:  ffi
Node Win32 Api
win32 api
Stars: ✭ 214 (+234.38%)
Mutual labels:  ffi
Tflite native
A Dart interface to TensorFlow Lite (tflite) through dart:ffi
Stars: ✭ 120 (+87.5%)
Mutual labels:  ffi
Libnotify
Ruby bindings for libnotify using FFI.
Stars: ✭ 138 (+115.63%)
Mutual labels:  ffi
Rust Ffi Guide
A guide for doing FFI using Rust
Stars: ✭ 207 (+223.44%)
Mutual labels:  ffi
Fficxx
Haskell-C++ Foreign Function Interface Generator
Stars: ✭ 117 (+82.81%)
Mutual labels:  ffi
Pythonnet
Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers.
Stars: ✭ 2,873 (+4389.06%)
Mutual labels:  ffi
Derry
A script manager for Dart.
Stars: ✭ 107 (+67.19%)
Mutual labels:  ffi
Inline Java
Haskell/Java interop via inline Java code in Haskell modules.
Stars: ✭ 197 (+207.81%)
Mutual labels:  ffi
Curryrs
Bridge the gap between Haskell and Rust
Stars: ✭ 252 (+293.75%)
Mutual labels:  ffi
Python Mpv
Python interface to the awesome mpv media player
Stars: ✭ 245 (+282.81%)
Mutual labels:  ffi
Inline Rust
Use snippets of Rust inline in your Haskell programs
Stars: ✭ 210 (+228.13%)
Mutual labels:  ffi

CXX with CMake build system

CXX CMake CI

Update 1-16-2022: I added a dockerfile to help people reproducing the results reported here.

This is an example repo to setup cxx with the cmake build system.

The official demo used cargo to orchestrate the two build systems and place the main function inside the rust project.

In a lot of other applications, however, we want to embed rust into a large cpp project where we don't have a chance to choose build systems. This template repo shows how to use cmake with cxx.

The cmake files do the following things:

  1. Call cargo build [--release] to build the static library and the necessary c++ header/source files
  2. Create a library from the cxx generated source and link to the rust static library
  3. Link and include the libraray to the corresponding targets

Cross-language LTO (linking time optimization)

Why?

Calling rust function from c++ (and vice versa) is not zero-overhead because the LLVM optimizer by default will not optimize across shared libraries, let alone across different languages.

LTO (linking time optimization) allows the optimizers to perform optimization during linking time (instead of compile time), thus enables cross library optimization.

How?

cmake -DENABLE_LTO=ON -DCMAKE_BUILD_TYPE=Release ..
make -j
./main

The -DENABLE_LTO=ON will compile and link both libraries with proper parameters. Note that cross language LTO between rust and c/c++ is only possible with clang toolchain, meaning that you need to have a very recent clang/lld installed.

Example output

With LTO

Points {
    x: [
        1,
        2,
        3,
    ],
    y: [
        4,
        5,
        6,
    ],
}
"cpp expert!"
Calling rust function, time elapsed: 100 ns.
Calling c++ function, time elapsed: 100 ns.

Without LTO

Points {
    x: [
        1,
        2,
        3,
    ],
    y: [
        4,
        5,
        6,
    ],
}
"cpp expert!"
Calling rust function, time elapsed: 1176600 ns.
Calling c++ function, time elapsed: 100 ns.

Credits

The cmake files are largely inspired by Using unsafe for Fun and Profit.

I learned a lot about cross language LTO from this post: Closing the gap: cross-language LTO between Rust and C/C++

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].