Skip to content
tezvyn:
Menu

Rust's `bindgen`: Auto-Generate FFI to C/C++

Source: rust-lang.github.ioHardHow cards are made

bindgen is a translator that reads C/C++ headers and writes the unsafe Rust FFI code to call them. It's used to integrate Rust with existing C libraries, like system APIs or legacy code, saving you from writing bindings by hand.

Why it exists

Rust needs a way to interoperate with the vast ecosystem of existing C and C++ libraries. Manually writing Foreign Function Interface (FFI) bindings is tedious, repetitive, and highly error-prone, as you must perfectly match C types, struct layouts, and function signatures in Rust. bindgen was created to automate this entire process.

The mental model

Think of bindgen as a specialized compiler that takes C/C++ header files (.h) as input and outputs a Rust source file (.rs). This generated file contains the extern "C" blocks and #[repr(C)] structs needed to call the C code from Rust. It's a bridge-builder, automatically constructing the unsafe foundation for interoperability.

How it works

bindgen uses libclang to parse C/C++ headers. It analyzes the Abstract Syntax Tree (AST) to understand the types, structs, unions, and functions declared. From this, it generates equivalent Rust definitions. For example, a C struct CoolStruct becomes a Rust #[repr(C)] pub struct CoolStruct, and a C function cool_function becomes a declaration inside an extern "C" block. It typically runs as part of your crate's build process via a build.rs script.

When to use it

Use bindgen whenever you need to call into a C or C++ library from Rust. This is common for interacting with operating system APIs, using established libraries for tasks like graphics or physics, or gradually migrating a legacy C/C++ codebase to Rust by wrapping parts of it.

When not to use it

Don't use bindgen if a high-quality, community-maintained "-sys" crate already exists for the library you need. For example, instead of running bindgen on libgit2 yourself, use the libgit2-sys crate. Also, bindgen only creates the raw bindings; it doesn't create a high-level, safe, and idiomatic Rust wrapper. That is a separate, manual step.

One canonical example

Given a C header file wrapper.h containing typedef struct CoolStruct { int x; } CoolStruct; void cool_function(CoolStruct cs);, bindgen generates the corresponding Rust code. This includes a #[repr(C)] struct CoolStruct with a field x of type c_int, and an extern "C" block declaring pub fn cool_function(cs: mut CoolStruct). This allows your Rust code to create a CoolStruct, get a raw pointer to it, and pass it to the C function.

Interview question

Which statement accurately describes the nature of the Rust code produced by `bindgen`?

  • a.It compiles the C/C++ source code into Rust modules, eliminating the need for a C/C++ toolchain.
  • b.It consists of raw, unsafe Foreign Function Interface (FFI) bindings directly mirroring C/C++ declarations.Correct
  • c.It automatically manages memory and resources allocated by the C/C++ library within Rust.
  • d.It provides a safe, high-level, and idiomatic Rust API for the underlying C/C++ library.
Why?

The card states that bindgen generates "unsafe Rust FFI code" and "only creates the raw bindings," explicitly noting it "doesn't create a high-level, safe, and idiomatic Rust wrapper." Option D is a common misconception, as users often desire such wrappers, but bindgen's output is intentionally low-level and unsafe.

Just read this? Test yourself on what you have been reading.

Read the original → rust-lang.github.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles