← Back to context

Comment by dlivingston

2 months ago

You only hand-write function bindings in simple or well-constrained cases.

In general, the expectation is that you will use bindgen [0].

It's a very easy process:

1. Create a `build.rs` file in your Rust project, which defines pre-build actions. Use it to call bindgen on whatever headers you want to import, and optionally to define library linkage. This file is very simple and mainly boilerplate. [1]

2. Import your bindgen-generated Rust module... just use it. [2]

You can also skip step 1: bindgen is also a CLI tool, so if your C target is stable, you can just run bindgen once to generate the Rust interface module and move that right into your crate.

[0]: https://rust-lang.github.io/rust-bindgen/

[1]: https://rust-lang.github.io/rust-bindgen/tutorial-3.html

[2]: https://github.com/Charles-Schleich/Rust-Bindgen-Example/blo...