prebindgen

Attribute Macro prebindgen 

Source
#[prebindgen]
Expand description

Attribute macro that exports FFI definitions for use in language-specific binding crates.

All types and functions marked with this attribute can be made available in dependent crates as Rust source code for both binding generator processing (cbindgen, csbindgen, etc.) and for including into projects to make the compiler generate #[no_mangle] FFI exports for cdylib/staticlib targets.

§Usage

// Use with explicit group name
#[prebindgen("group_name")]
#[repr(C)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

// Use with default group name "default"
#[prebindgen]
pub fn calculate_distance(p1: &Point, p2: &Point) -> f64 {
    ((p2.x - p1.x).powi(2) + (p2.y - p1.y).powi(2)).sqrt()
}

// Add cfg attribute to generated code
#[prebindgen(cfg = "feature = \"experimental\"")]
pub fn experimental_function() -> i32 {
    42
}

// Combine group name with cfg
#[prebindgen("functions", cfg = "unix")]
pub fn another_function() -> i32 {
    42
}

§Requirements

  • Must call prebindgen::init_prebindgen_out_dir() in your crate’s build.rs
  • Optionally takes a string literal group name for organization (defaults to “default”)
  • Optionally takes cfg = "condition" to add #[cfg(condition)] to generated code