binding generator for rust language
Rust C Other
Latest commit 7ed6c24 Dec 4, 2016 @birkenfeld birkenfeld committed with Allow multiple --link args again. (#403)
Also removes the extra usage line for --match which is unnecessary.


A native binding generator for the Rust language.

rust-bindgen was originally ported from clay's bindgen.



  • Clang >= 3.5


$ cargo install bindgen

Bindgen will be dynamically linked to your default clang version. See clang-sys if you want to use an other version or do a static link build. The clang-sys feature static can be activated via the bindgen feature clang_sys/static.


Command Line

$ bindgen <header> [<bindgen options>] [-- <clang options>]

See --help for a list of the supported options.


    bindgen!(header, options...)

The use of this plugin requires the use of a nightly compiler.


Option Name Type Default
link str
match str
builtins bool true
allow_unknown_types bool false
clang_args str


Generate a Lua binding with the CLI

bindgen --link lua --builtins /usr/include/lua.h -o

Generate a Lua binding with the plugin


bindgen = "*"


mod lua_bindings {
    bindgen!("/usr/include/lua.h", link="lua", builtins=true)

Using a build script to generate bindings at compile time

Due to a known issue with include! ( when generating bindings in a build script and importing them with include!, you'll want to wrap the bindings in a module before writing them to a file to avoid triggering the issue with top-level attributes in include!. Some more discussion about this issue can be found here.


name = "bindgen_ex"
build = ""

bindgen = "0.19.0"

extern crate bindgen;

use std::io::prelude::*;
use std::fs::File;

fn main(){
    let mut bindings = bindgen::Builder::new("my_lib.h");"my_lib", bindgen::LinkType::Static);
    // Generate the bindings to a string so we can wrap them
    // instead of going through the `write_to_file` API.
    let generated_bindings = bindings.generate().expect("Failed to generate bindings");
    // Now open the file we'll write the generated bindings too
    let mut file = File::create("").expect("Failed to open file");
    // Wrap the bindings in a `pub mod` before writing bindgen's output
    file.write(format!("pub mod {} {{\n", "my_lib").as_bytes()).unwrap();


fn main() {