LinuxCommandLibrary

cbindgen

TLDR

Generate C header from Rust library

$ cbindgen -o [header.h]
copy
Generate C++ header
$ cbindgen --lang c++ -o [header.hpp]
copy
Generate C header explicitly
$ cbindgen --lang c -o [header.h]
copy
Use specific config file
$ cbindgen --config [cbindgen.toml] -o [header.h]
copy
Generate from specific crate
$ cbindgen --crate [crate_name] -o [header.h]
copy
Show help
$ cbindgen --help
copy

SYNOPSIS

cbindgen [options] [cratedirectory_]

DESCRIPTION

cbindgen generates C and C++11 headers from Rust libraries that expose a public C API. It parses Rust source code and creates corresponding C declarations for FFI-exported types and functions.
Developed by Mozilla, it ensures generated headers match Rust's type layout and ABI guarantees.

PARAMETERS

-o, --output file

Output header file path
--lang language
Output language: c or c++ (default: c++)
--config file
Path to cbindgen.toml configuration
--crate name
Crate name to generate bindings for
--profile name
Cargo profile to use
-v, --verbose
Enable verbose output
-q, --quiet
Suppress output
--verify
Verify existing header matches generated
--help
Show help

CONFIGURATION

Create cbindgen.toml for detailed configuration:

$ language = "C"
header = "/* Generated by cbindgen */"
include_guard = "MY_HEADER_H"
copy

BUILD SCRIPT USAGE

Add to build.rs for automatic generation on build.

CAVEATS

Only generates headers for pub extern "C" functions and #[repr(C)] types. Requires proper FFI annotations in Rust code. Configuration file customizes output format.

SEE ALSO

cargo(1), bindgen(1), rustc(1)

Copied to clipboard