Auto-generating a C Header

Manually writing headers or function entries for every Rust function you create is ok for a tiny single-function project, but gets pretty painful when you are exporting a larger project.

Note that I often maintain the headers myself, especially when dealing with languages other than C. So this is optional, but can save you some time. We've also switch to ex04_rust_headers in the repo.

Add cbindgen to Cargo.toml:

[build-dependencies]
cbindgen = "0.24"

And add a build.rs:

fn main() {
    // Generate the header file
    cbindgen::generate(".")
        .expect("Unable to generate bindings")
        .write_to_file("bindings.h");
}

When you build the project, as well as your dynamic library - you will find a bindings.h file:

Note: This requires libclang to work. That can be a little painful on Windows! There's a Docker version if you're stuck.

#ifndef MY_PROJECT_BINDINGS_H
#define MY_PROJECT_BINDINGS_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

void say_hello(void);

#endif /* MY_PROJECT_BINDINGS_H */