Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the include path with bindgen

Tags:

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.

like image 860
Chris Emerson Avatar asked Mar 11 '17 23:03

Chris Emerson


1 Answers

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 
like image 124
Josh Stone Avatar answered Sep 21 '22 06:09

Josh Stone