Rust C Binding
Created
2024 May 19 12:48Library member & application member
- Set up the build script:
- Create a
build.rs file in the application directory.
- Add the necessary lines to link against the library.
- Prepare the C header file:
- Create a
wrapper.h like header file in the library directory.
- Include the required C header file in
wrapper.h.
- Generate Rust bindings:
- Use bindgen to generate the bindings during the build process.
- Bindgen will read
wrapper.h and create a Rust file with the bindings, typically placed in the OUT_DIR.
- Include the bindings in Rust code:
- In library
lib.rs, include the generated bindings using include!(concat!(env!("OUT_DIR"), "/bindings.rs"));.
- Use the C function in Rust:
- In your Rust application, declare the C function using
extern "C".
- You can now call
c function within your Rust code.