๐Ÿ“ฆ rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.

โ˜… 5.1k stars โ‘‚ 786 forks ๐Ÿ‘ 5.1k watching โš–๏ธ BSD 3-Clause "New" or "Revised" License
bindingscodegenffi
๐Ÿ“ฅ Clone https://github.com/rust-lang/rust-bindgen.git
HTTPS git clone https://github.com/rust-lang/rust-bindgen.git
SSH git clone git@github.com:rust-lang/rust-bindgen.git
CLI gh repo clone rust-lang/rust-bindgen
luohewuyang luohewuyang chore: fix some typos in comments 74a5f30 29 days ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ bindgen
๐Ÿ“ bindgen-cli
๐Ÿ“ bindgen-tests
๐Ÿ“ book
๐Ÿ“ ci
๐Ÿ“ csmith-fuzzing
๐Ÿ“ releases
๐Ÿ“„ .gitattributes
๐Ÿ“„ .gitignore
๐Ÿ“„ appveyor.yml
๐Ÿ“„ Cargo.lock
๐Ÿ“„ Cargo.toml
๐Ÿ“„ CHANGELOG.md
๐Ÿ“„ clippy.toml
๐Ÿ“„ CONTRIBUTING.md
๐Ÿ“„ LICENSE
๐Ÿ“„ README.md
๐Ÿ“„ rustfmt.toml
๐Ÿ“„ triagebot.toml
๐Ÿ“„ README.md

crates.io docs.rs

bindgen

bindgen automatically generates Rust FFI bindings to C (and some C++) libraries.

For example, given the C header doggo.h:

typedef struct Doggo {
    int many;
    char wow;
} Doggo;

void eleven_out_of_ten_majestic_af(Doggo* pupper);

bindgen produces Rust FFI code allowing you to call into the doggo library's functions and use its types:

/* automatically generated by rust-bindgen 0.99.9 */

#[repr(C)]
pub struct Doggo {
    pub many: ::std::os::raw::c_int,
    pub wow: ::std::os::raw::c_char,
}

extern "C" {
    pub fn eleven_out_of_ten_majestic_af(pupper: *mut Doggo);
}

Users Guide

๐Ÿ“š Read the bindgen users guide here! ๐Ÿ“š

MSRV

The bindgen minimum supported Rust version is 1.70.0.

The bindgen-cli minimum supported Rust version is 1.70.0.

No MSRV bump policy has been established yet, so MSRV may increase in any release.

The MSRV is the minimum Rust version that can be used to compile each crate. However, bindgen and bindgen-cli can generate bindings that are compatible with Rust versions below the current MSRV.

Most of the time, the bindgen-cli crate will have a more recent MSRV than bindgen as crates such as clap require it.

API Reference

API reference documentation is on docs.rs

Environment Variables

In addition to the library API and executable command-line API, bindgen can be controlled through environment variables.

End-users should set these environment variables to modify bindgen's behavior without modifying the source code of direct consumers of bindgen.

  • BINDGEN_EXTRA_CLANG_ARGS: extra arguments to pass to clang
  • Arguments are whitespace-separated
  • Use shell-style quoting to pass through whitespace
  • Examples:
  • Specify alternate sysroot: --sysroot=/path/to/sysroot
  • Add include search path with spaces: -I"/path/with spaces"
  • BINDGEN_EXTRA_CLANG_ARGS_<TARGET>: similar to BINDGEN_EXTRA_CLANG_ARGS,
but used to set per-target arguments to pass to clang. Useful to set system include directories in a target-specific way in cross-compilation environments with multiple targets. Has precedence over BINDGEN_EXTRA_CLANG_ARGS.

Additionally, bindgen uses libclang to parse C and C++ header files. To modify how bindgen searches for libclang, see the clang-sys documentation. For more details on how bindgen uses libclang, see the bindgen users guide.

Releases

We don't follow a specific release calendar, but if you need a release please file an issue requesting that (ping @emilio for increased effectiveness).

Contributing

See CONTRIBUTING.md for hacking on bindgen!