8000 GitHub - flyingyizi/rust_with_c_lib: demo rust how to bind c library build with cmake
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

flyingyizi/rust_with_c_lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 overview

how to binding non-rust code is descripted in cargo-build-scripts-documents.

below 78F2 records some key points that I recommend.

where to locate build script

Create build.rs file in the root of the project. or specify in Cargo.toml file, e.g. build = "<path to build.rs>

binding name appoint

*-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").

Inputs to the Build Script

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.

build script how to commuicate with cargo

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(":"));

generate Rust FFI bindings to C

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.

notes for cmake-rs

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

About

demo rust how to bind c library build with cmake

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0