An example of integrating cargo/rust with CMake projects, and a reusable cmake function to help.
This builds arbitrary rust projects under your ${CMAKE_BINARY_DIR} output directory.
- CMakeLists.txt is the top level CMake configuration file
- rust.cmake defines a function to build rust libraries
- bin/ contains the C source files
- rs/ contains the example rust library
git clone git@github.com:ryankurte/rust-cmake.gitto clonecd rust-cmaketo change to the project directorymkdir build && cd buildto create a build directory and change to itcmake ..to configure cmakemake testlibto perform a first library build (see issues section following)maketo build everything./exampleto execute the example application
- Copy rust.cmake into your project
- Include it in your top level CMake file with
include(rust.cmake) - Add a rust project with
build_rust(NAME LOCATION TARGET), target can be""for native compilation. - Add
${RUST_LIBS}to your linker arguments withtarget_link_libraries(example ${RUST_LIBS}).
- The c header file is not generated until the rust library is built, which must be manually run the first time. There should be a way to order this.