使用bindgen设置包含路径 [英] Setting the include path with bindgen
问题描述
我正在为一个小型C库编写一个Rust接口,该标头分布在几个位置.它不是系统库,通常由同一软件包中的某些可执行文件使用.我目前将其作为git子模块包含在我的Cargo项目中.
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.
构建库似乎很容易;我选择使用build.rs
中的gcc
板条箱:
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");
现在我希望使用 bindgen
板条箱生成FFI接口时不会大惊小怪,但似乎没有设置包含路径的方法.
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.
我可以按照该博客并包含多个标头,但是如果dir1/dir1.h
直接包含conf.h
,则由于.include("external/foo/config_a/")
而无法在构建时起作用.
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.
我在bindgen
的API中找不到任何东西可以为您提供帮助(本质上,我想传递gcc/clang的-I
选项的等效项).我想念什么吗?
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?
到目前为止,我能想到的最好的选择是将库源中的各种标头复制到build.rs
中的某个临时目录中,然后在其中运行bindgen
,但是如果有更好的方法,这似乎有些混乱. /p>
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
这篇关于使用bindgen设置包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!