为什么Rust build.rs无法找到C头文件? [英] Why Rust build.rs is unable to find C headers?
问题描述
基本上,我正在尝试构建一个带有build.rs文件的板条箱.这个板条箱位于一个较大的项目中,应该是lib板条箱.在这个build.rs文件中,我正在尝试编译一个包含几个标头的.C文件.
Basically I am trying to cargo build a crate which has a build.rs file. This crate is located inside a bigger project and it's supposed to be a lib crate. And inside this build.rs file, I am trying to compile a .C file which includes a couple of headers.
有趣的事实:我从另一个小演示箱中获得了这个build.rs文件和箱结构,在该演示箱中,我可以使用这些头文件编译这个精确的C文件没有问题.
Fun fact: I got this build.rs file and crate structure from another little demo crate, and in that demo crate I had no problem to compile this exact C file with these headers.
此处完全错误:
这里是指向github的链接: https://github.com/mihaidogaru2537/FirecrackerPlayground/tree/dpdk_component/firecracker/src/dpdk_component
Here is a link to github: https://github.com/mihaidogaru2537/FirecrackerPlayground/tree/dpdk_component/firecracker/src/dpdk_component
我正在谈论一个板条箱,您还可以看到它所在的更大项目.在README文件中,您可以看到完整的错误.
There is the crate I am talking about and you can also see the bigger project in which it resides. In the README file you can see the full error.
我是从大型项目的根部还是从这个有问题的板条箱的根部进行货物建造,错误都是相同的.
Either I do cargo build from the root of the big project or from the root of this problematic crate, the error is the same.
" cargo:warning =/usr/include/asm-generic/errno.h:5:10:致命错误:asm-generic/errno-base.h:没有这样的文件或目录货物:警告= 5 |#include< asm-generic/errno-base.h>"
"cargo:warning=/usr/include/asm-generic/errno.h:5:10: fatal error: asm-generic/errno-base.h: No such file or directory cargo:warning= 5 | #include <asm-generic/errno-base.h>"
丢失的文件可能会根据我正在build.rs内部进行的.flag(-I/path/..")调用而改变
The missing file might change depending on the .flag("-I/path/..") calls I am doing inside the build.rs
如您所见,目前无法找到errno-base.h,但我包括了asm-generic的路径.
As you can see, right now it's unable to find errno-base.h, but I am including the path to asm-generic.
这是来自Crate的Crate箱中的build.rs文件的代码,如您所见,在调用compile之前,我不必添加任何包含标志.
Here is the code of the build.rs file from the crate where the compilation of this C file works, as you can see, I did not have to add any include flags before calling compile.
fn main() {
// Tell cargo to tell rustc to link the system bzip2
// shared library.
// println!("cargo:rustc-link-lib=rte_ring");
// println!("cargo:rustc-link-lib=rte_mempool");
// Tell cargo to invalidate the built crate whenever the wrapper changes
// println!("cargo:rerun-if-changed=wrapper.h");
let _src = ["src/static-functions.c"];
println!("cargo:rerun-if-changed=build.rs");
let mut builder = cc::Build::new();
let build = builder
.file("src/static-functions.c")
.flag("-Wno-unused-parameter");
build.compile("foo");
}
其他信息:
-
有问题的板条箱很小,请参见上面的链接.在include目录中有build.rs文件,C文件和头文件.
The problematic crate is pretty small, see the link above. There is the build.rs file, C file and header file is inside the include directory.
我怀疑的一件事是更大项目的目标:
One thing that I suspect, is that the target of the bigger project:
TARGET = Some("x86_64-unknown-linux-musl")
TARGET = Some("x86_64-unknown-linux-musl")
可能会影响C文件的编译方式.
might affect the way the C file is compiled.
在进行编译的项目中,我没有使用linux-musl.
In the project where the compilation is working, I am not using that linux-musl stuff.
-
在谈到Rust时,我完全是个菜鸟,但是我对C/C ++的工作方式确实有很好的了解.
I am a total noob when it comes to Rust, but I do have a decent understanding of how C/C++ works.
我正在Ubuntu 20.04上运行项目
I am running the project on Ubuntu 20.04
那些丢失的标头是由于导入了DPDK标头而导致的,我在有问题的计算机上安装了DPDK库.
Those missing headers are a result of importing DPDK headers, I have DPDK libraries installed on the machine in question.
如果您有任何问题,请告诉我,对不起,感谢您的长时间阅读.
Let me know if you have any questions, sorry for the long read and thank you.
推荐答案
我通过调整cargo build命令以使用x86_64-unknown-linux-gnu而不是x86_64-unknown-linux-musl(默认情况下,货物建造以某种方式达到了预期目标)
I somehow managed to fix it by adjusting the cargo build command to use x86_64-unknown-linux-gnu as target instead of x86_64-unknown-linux-musl (By default cargo build was doing the musl target somehow)
因此,如果您尝试构建使用DPDK库的rust应用程序,并且缺少标题,请确保尝试以下操作:
So if you are trying to build a rust app which is using DPDK libraries and you are getting missing headers, make sure to try the following:
货物建造--target = x86_64-unknown-linux-gnu
好吧,如果您必须使用musl并且别无选择,我没有答案.但是对我来说这就足够了.
Well if you have to use musl and there is no alternative, I don't have an answer. But to me this was enough.
如果有人解释了为什么在这种情况下musl无法正常工作,请告诉我们.
If someone has an explanation why musl is nor working in this scenario, please let us know.
Reddit Rust社区也为我提供了帮助,如果您感兴趣,请查看此链接: https://www.reddit.com/r/rust/comments/mo3i08/unable_to_compile_c_file_inside_buildrs_headers/
Reddit Rust community helped me as well, check this link out if you are interested: https://www.reddit.com/r/rust/comments/mo3i08/unable_to_compile_c_file_inside_buildrs_headers/
所以为什么build.rs无法找到.C标头?
答案
因为我使用x86_64-unknown-linux-musl作为目标.
Because I was using x86_64-unknown-linux-musl as target.
这篇关于为什么Rust build.rs无法找到C头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!