talira

Generates Rust FFI bindings to C and some C++ libraries from their headers

open sourceBSD-3-Clausefree

bindgen reads C (and some C++) header files with libclang and generates the Rust FFI code to call into the library and use its types: repr(C) structs, extern function declarations, enums and constants. Most projects run it from a build.rs script as a build dependency, so bindings are generated at compile time; the bindgen-cli crate does the same from the command line. Options control which items are allowlisted or blocklisted, which types stay opaque or are replaced, and which traits are derived, and environment variables pass extra arguments to clang, per target for cross-compilation. It's maintained under the Rust project's GitHub organization.

Ways to install

Package managers

Problems it solves

  • call a C library from Rust without writing the bindings by hand
  • generate Rust types from a C header file at build time
  • wrap a system library like libgit2 or SQLite in a Rust crate

Features

  • Generates repr(C) structs, extern functions, enums and constants from C headers
  • Runs from build.rs as a build dependency, or from the command line with bindgen-cli
  • Allowlisting, blocklisting, opaque and replaced types
  • Control over derived traits such as Copy and Clone
  • Extra clang arguments through environment variables, per target for cross-compiling

Limitations

  • C++ support is partial: it handles C and only some C++2
  • Needs libclang installed to parse headers3

Alternatives

The same job

These do what bindgen does.

  1. flutter_rust_bridge Call Rust code from Flutter and Dart, with the glue code generated for you
  2. napi-rs A framework for building compiled Node.js add-ons in Rust via Node-API
  3. GObject Introspection Middleware between GObject-based C libraries and language bindings
  4. wasm-bindgen Generates the glue between Rust WebAssembly modules and JavaScript

Compare bindgen with the top 3