← 返回命令列表

Linux command

bindgen 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Generate Rust bindings

bindgen [input.h] -o [bindings.rs]

Generate bindings with allowlist

bindgen [input.h] --allowlist-function "[regex]" --allowlist-type "[regex]" -o [bindings.rs]

Generate bindings with blocklist

bindgen [input.h] --blocklist-type "[type_name]" -o [bindings.rs]

Add include path

bindgen [input.h] -o [bindings.rs] -- -I[/path/to/includes]

Generate bindings

bindgen [input.hpp] --enable-cxx-namespaces -o [bindings.rs]

Use specific clang arguments

bindgen [input.h] -o [bindings.rs] -- -std=c11 -DFOO=1

Generate with derive traits

bindgen [input.h] --with-derive-default --with-derive-eq -o [bindings.rs]

说明

bindgen automatically generates Rust FFI (Foreign Function Interface) bindings to C and C++ libraries. It parses C/C++ header files using libclang and produces Rust code that allows calling native functions from Rust. The generated bindings include function declarations, struct definitions, enums, constants, and type aliases. bindgen handles complex types like function pointers, unions, and opaque types, mapping them to appropriate Rust equivalents. Typically used in build.rs scripts to generate bindings at compile time, ensuring bindings stay synchronized with library headers. It's a key tool in the Rust ecosystem for interfacing with existing C/C++ code.

参数

-o _file_
Write output to file instead of stdout.
--allowlist-function _regex_
Only generate bindings for matching functions.
--allowlist-type _regex_
Only generate bindings for matching types.
--blocklist-type _regex_
Exclude matching types from bindings.
--blocklist-function _regex_
Exclude matching functions.
--enable-cxx-namespaces
Enable C++ namespace support.
--with-derive-default
Add Default derive to structs.
--with-derive-eq
Add Eq derive to applicable types.
--with-derive-hash
Add Hash derive to applicable types.
--no-layout-tests
Skip generating layout tests.
--generate _items_
Specify what to generate (functions, types, vars, methods).
--opaque-type _regex_
Treat matching types as opaque.
--wrap-static-fns
Generate wrappers for static and static inline functions.
--rust-target _version_
Minimum supported Rust version for the generated bindings.
--
Separator for clang arguments passed to libclang.

FAQ

What is the bindgen command used for?

bindgen automatically generates Rust FFI (Foreign Function Interface) bindings to C and C++ libraries. It parses C/C++ header files using libclang and produces Rust code that allows calling native functions from Rust. The generated bindings include function declarations, struct definitions, enums, constants, and type aliases. bindgen handles complex types like function pointers, unions, and opaque types, mapping them to appropriate Rust equivalents. Typically used in build.rs scripts to generate bindings at compile time, ensuring bindings stay synchronized with library headers. It's a key tool in the Rust ecosystem for interfacing with existing C/C++ code.

How do I run a basic bindgen example?

Run `bindgen [input.h] -o [bindings.rs]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -o _file_ do in bindgen?

Write output to file instead of stdout.