如何在构建脚本中指定链接器标志/参数? [英] How can I specify linker flags/arguments in a build script?
问题描述
我正在使用Rust, bindgen 和
I'm using Rust, bindgen, and a build script to work on some FFI bindings to a library.
此库是使用 OpenMP 构建的,因此在链接该库时,通常会传递
This library is built using OpenMP, so when linking against it, I'd normally pass the -fopenmp
flag to the compiler.
当库由Cargo构建时,如何由build.rs
设置此标志?
How can I get this flag to be set by build.rs
when the library is built by Cargo?
当前,使用Cargo构建失败,失败命令如下:
Currently, building using Cargo fails, with the failing command being something like:
cc -Wl,--as-needed -Wl,-z,noexecstack -m64 -l gomp -l stdc++
...skipping dozens of paths/files...
-Wl,-Bdynamic -l dl -l rt -l pthread -l gcc_s -l c -l m -l rt -l pthread -l util
失败,并显示数百个undefined reference to 'GOMP_parallel_end'
错误.
which fails with hundreds of undefined reference to 'GOMP_parallel_end'
errors.
使用手动添加的-fopenmp
标志重新运行上面生成的命令.
Rerunning the generated command above with the -fopenmp
flag manually added succeeds.
我可以在编译前使用RUSTFLAGS='-C link-args=-fopenmp'
指定标志,但是有没有办法从build.rs
内部指定标志?
I can specify the flag using RUSTFLAGS='-C link-args=-fopenmp'
before compiling, but is there a way of specifying it from within build.rs
?
推荐答案
您不能.相反,您可以使用货运配置文件.
You cannot. Instead, you can use a Cargo configuration file.
[build]
rustflags = ["-C", "link-args=-fsome-artisanal-option"]
执行
$ cargo build --verbose
Compiling example v0.1.0 (file:///private/tmp/example)
Running `rustc ...blah blah blah... -C link-args=-fsome-artisanal-option`
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-m64" ...blah blah blah... "-fsome-artisanal-option"
= note: clang: error: unknown argument: '-fsome-artisanal-option'
另请参阅:
- How to get the linker to produce a map file using Cargo
- How can I globally configure a Cargo profile option?
- Is it possible to specify `panic = "abort"` for a specific target?
这篇关于如何在构建脚本中指定链接器标志/参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!