demo how to binding c with your rust program. to simplest implement, this project guess that cmake command is in the %PATH%. and use visual studio as the generator.
how to binding non-rust code is descripted in cargo-build-scripts-documents.
below 78F2 records some key points that I recommend.
Create build.rs file in the root of the project. or specify in Cargo.toml file, e.g. build = "<path to build.rs>
*-sys
is a naming convention for crates that help Rust programs use C ("system") libraries, e.g. libz-sys, kernel32-sys, lcms2-sys. The task of the sys crates is expose a minimal low-level C interface to Rust (FFI) and to tell Cargo how to link with the library. Adding higher-level, more Rust-friendly interfaces for the libraries is left to "wrapper" crates built as a layer on top of the sys crates (e.g. a "rusty-image-app" may depend on high-level "png-rs", which depends on low-level "libpng-sys", which depends on "libz-sys").
When the build script is run, there are a number of inputs to the build script, all passed in the form of environment variables.
In addition to environment variables, the build script’s current directory is the source directory of the build script’s package.
The script may communicate with Cargo by printing specially formatted commands prefixed with cargo:
to stdout.
full list see outputs-of-the-build-script.
notes: "cargo:KEY=VALUE
— Metadata, used by links scripts." This metadata is passed to the build scripts of dependent packages. details refer to the-links-manifest-key. for example, the sdl2-sys crate add DEP_SDL2_INCLUDE
in sdl2-sys
through println!("cargo:include={}", include_paths.join(":"));
in this sample, we use bindgen to got it. then use include!(concat!(env!("OUT_DIR"), "/clibsample_bindings.rs"));
statement merge them into our rust project.
if using visual studio generator in cmake, defaultly not generate "install" build target. only when using "install(TARGETS xxx DESTINATION .)"
statement in cmakefile.txt, then the "install" build target will be existed.
for the cmake-rs crate, if not set the build target, the default is "install", "install" build target is defaultly suppoted by by make and ninja. but visual studio not support it.
so if the generator is visual studio:
- you need set the appropriate build target, e.g.
cfg.build_target("ALL_BUILD").build();
- or using install statement in cmakefile.txt