Setting the include path with bindgen

痞子三分冷 提交于 2020-03-18 11:22:56

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/42741815/setting-the-include-path-with-bindgen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!