29

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Chris Emerson
  • 13,041
  • 3
  • 44
  • 66

1 Answers1

38

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
hellow
  • 12,430
  • 7
  • 56
  • 79
Josh Stone
  • 841
  • 9
  • 7