I'm writing a Rust interface to a small C library, which has headers spread in a few locations. It's not a system library, and is normally used by some executables in the same package; I'm currently including it as a git submodule in my Cargo project.
Building the library seems to be pretty easy; I've opted to use the gcc
crate from build.rs
:
gcc::Config::new() .file("external/foo/dir1/file1.c") .file("external/foo/dir2/file2.c") .include("external/foo/dir1/") .include("external/foo/dir2/") .include("external/foo/config_a/") .compile("libfoo.a");
Now I was hoping to use the bindgen
crate to generate the FFI interface without too much fuss, but it doesn't seem to have a way of setting include paths.
I can create a wrapper.h
as suggested by this blog and include several headers, but if dir1/dir1.h
includes conf.h
directly, which works when building due to .include("external/foo/config_a/")
it can't be found.
I can't find anything in bindgen
's API to help here (essentially I want to pass the equivalent of gcc/clang's -I
option). Am I missing anything?
The best option I can think of so far is to copy the various headers from the library source into some temporary directory in build.rs
and run bindgen
on that, but that seems somewhat messy if there's a nicer way.
With the API you can use Builder::clang_arg
with arbitrary arguments:
let b = bindgen::builder().header("foo.h").clang_arg("-I/path");
From the command line you can do the same by appending arguments after --
, like:
bindgen foo.h -- -I/path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With