Automatic creation of the bindings using bindgen

Exercise 1.a: Create a new library crate named zstd-sys inside the cargo workspace.

Don't forget to add the name of the library to the members list in your workspace definition file.

⚠️ For the rest of this page, everything happens in the zstd-sys crate.

Exercise 1.b: Add bindgen to the build-dependencies section of Cargo.toml. You can use cargo add with the appropriate options to do so.

Exercise 1.c: Create a src/header.h file containing

#include <zstd.h>

Exercise 1.d: Create a file next to Cargo.toml. Using the example given in class, add the code to generate the bindings. The library you want to link is zstd, and all types and functions start with ZSTD_.

Exercise 1.e: In src/, include the file generated by bindgen.

Since the zstd library uses global variables whose name is not in uppercase, and some function names are not in snake case (words_separated_by_underscores_), you can add this near the top of your src/ file:


Check that your library builds without errors using cargo build.

Exercise 1.f (optional): Install cargo-expand (cargo install cargo-expand), and look at the code generated by bindgen using cargo expand --lib. To use it you need to use the nightly Rust toolchain. If you don't want to do that, you may also look for the file in target and display its content (assuming you are in the zstd-sys crate root):

$ find ../target -name -exec cat {} \;