I needed to create this to prove to myself that it is possible (it is) so if this helps then I'm glad. As you might expect, a library wraps a bindings library which contains the C/C++/CMake project:
.
βββ Cargo.lock
βββ Cargo.toml
βββ hello_lib
βΒ Β βββ Cargo.lock
βΒ Β βββ Cargo.toml
βΒ Β βββ libhello_bindings
βΒ Β βΒ Β βββ Cargo.lock
βΒ Β βΒ Β βββ Cargo.toml
βΒ Β βΒ Β βββ build.rs
βΒ Β βΒ Β βββ libhello
βΒ Β βΒ Β βΒ Β βββ CMakeLists.txt
βΒ Β βΒ Β βΒ Β βββ hello.c
βΒ Β βΒ Β βΒ Β βββ proto.h
βΒ Β βΒ Β βββ src
βΒ Β βΒ Β βββ lib.rs
βΒ Β βββ src
βΒ Β βββ lib.rs
βββ src
βββ main.rs
the file build.rs is responsible for first executing the CMake build as well as generating the Rust FFI bindings which are used (and tested with cargo test)
in the hello_lib sub-crate.
- Cargo build scripts: https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/cargo/reference/build-scripts.html
- cmake crate: https://docs.rs/cmake/latest/cmake/
- bindgen crate: https://crates.io/crates/bindgen/0.19.2
- bindgen documentation: https://rust-lang.github.io/rust-bindgen/
- Rust linkage: https://doc.rust-lang.org/reference/linkage.html