cheadergen 0.3.0

Annotations for controlling C/C++ header generation from Rust.
Documentation

cheadergen

cheadergen generates accurate C headers for Rust libraries that expose a C-compatible API.

cheadergen provides:

  • Multi-crate support. One C header per crate, with cross-crate #includes wired up automatically.
  • Compiler-accurate type analysis. Type information comes from rustdoc-json, so the generated output mirrors what the Rust compiler actually sees.
  • Macro-aware. Items defined by declarative or procedural macros are picked up automatically.

cheadergen is an alternative to cbindgen. Check out our comparison page for more details.

cheadergen (C Header Generator) is built and maintained by Luca Palmieri.

What it does

You write Rust:

#[repr(C)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

#[unsafe(no_mangle)]
pub extern "C" fn distance(a: Point, b: Point) -> f64 {
    ((a.x - b.x).powi(2) + (a.y - b.y).powi(2)).sqrt()
}

cheadergen produces a header your C code can consume:

typedef struct {
    double x;
    double y;
} Point;

double distance(Point a, Point b);

New to C/Rust interoperability?

If you've never written extern "C" in Rust before, the FFI chapter of the Rust Nomicon is a good primer on the topic.

Documentation