Rust C Binding

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2024 May 19 12:48
Editor
Edited
Edited
2025 Jun 21 18:52
Refs
Refs
Rust FFI

Library member & application member

  1. Set up the build script:
      • Create a build.rs file in the application directory.
      • Add the necessary lines to link against the library.
  1. 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.
  1. 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.
  1. Include the bindings in Rust code:
      • In library lib.rs, include the generated bindings using include!(concat!(env!("OUT_DIR"), "/bindings.rs"));.
  1. 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.
 
 
 
 

Recommendations