Rust-bindgen忽略头文件中包含特定的include



我有一个头文件,让我们问候。h:

include <hello.h>;
include <bye.h>;
include <hola.h>;
...

我在rust中使用bindgen生成那些从c头到rust的文件。但我想忽略生成include <hola.h>头,而只使用helllo.hbye.h生成问候语.h。我在docs.rs的bindgen文档中搜索过它,但没有找到任何提示。或者有没有选择用叮当声来做到这一点

我通过猜测我试图从c绑定到rust的库是"来解决这个问题;命名空间";,在C语言中实际上没有名称空间,但编写开源代码的人倾向于在他们的代码前面加上";some_prefix*";。因此,如果我想要绑定的h文件看起来像:

mylib.h:

include <hello.h>;
include <bye.h>;
include <hola.h>;
...
mylib_add_value(...);
mylib_remove_value(...);
mylib_display_value(...);
...

我不希望生成hola.h,我可以用宾登的允许和阻止功能过滤输出,比如:

build.rs:

fn main() {
// Tell cargo to tell rustc to link the system nvpair of zfs
// shared library.
println!("cargo:rustc-link-lib=mylib");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
.allowlist_var(r#"(w*mylibw*)"#)
.allowlist_type(r#"(w*mylibw*)"#)
.allowlist_function(r#"(w*mylibw*)"#)
.blocklist_item(r#"(w*holaw*)"#)
.blocklist_type(r#"(w*holaw*)"#)
.blocklist_function(r#"(w*holaw*)"#)
.clang_args(vec!["-I/usr/include/mylib"])
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: (true),
})
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
//.write_to_file(out_path)
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

Bindgen将首先带来allowlist的所有依赖项,如果其中一些依赖项包括*hola*,则不会生成它

最新更新