[英]How do I change the default rustc / Cargo linker?
I would like to make rustc use lld
as a linker instead of ld
in a particular crate.我想让 rustc 使用lld
作为链接器,而不是特定板条箱中的ld
。 So I create .cargo/config
in my project directory with the following:所以我在我的项目目录中创建了.cargo/config
,内容如下:
[target.x86_64-unknown-linux-gnu]
linker = "ld.lld"
Which leads to linker errors:这会导致链接器错误:
$ cargo build
...
= note: ld.lld: error: unable to find library -ldl
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lm
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find library -lutil
ld.lld: error: unable to find library -lutil
Same thing with rust-lld
. rust-lld
。 If I set linker = "ld"
(which should be the default, right?), I just get如果我设置linker = "ld"
(这应该是默认值,对吧?),我就得到
= note: ld: cannot find -lgcc_s
I tried to resolve all the missing libraries manually (with -C link-arg=--library-path=/usr/lib/x86_64-linux-gnu
and the like), but it only lead to wrong linkage and a segfaulting binary.我试图手动解决所有丢失的库(使用-C link-arg=--library-path=/usr/lib/x86_64-linux-gnu
等),但它只会导致错误的链接和段错误的二进制文件。
Interestingly enough, if I replace /usr/bin/ld
with a symlink to /usr/bin/ld.lld
, it works great (no errors, and from the compiled binary I see that it was indeed linked with lld
).有趣的是,如果我用/usr/bin/ld.lld
的符号链接替换/usr/bin/ld
,它运行良好(没有错误,并且从编译的二进制文件中我看到它确实与lld
链接)。 However, I don't want to make lld
my system-wide linker, I just want to use it in a particular Rust crate.但是,我不想让lld
我的系统范围的链接器,我只想在特定的 Rust crate 中使用它。
So what is the proper way to change the default rustc linker?那么更改默认 rustc 链接器的正确方法是什么?
Thanks to @Jmb comment, I found a solution.感谢@Jmb 评论,我找到了解决方案。 Turns out that the default linker that rustc
uses is actually cc
(which makes sense - it supplies all the needed defaults to compile/link C code, which also work for Rust).事实证明, rustc
使用的默认链接器实际上是cc
(这是有道理的——它提供了编译/链接 C 代码所需的所有默认值,这也适用于 Rust)。 We can pass an argument to cc
to make it link with lld
:我们可以将参数传递给cc
以使其与lld
链接:
[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C", "link-arg=-fuse-ld=lld",
]
Now cargo build
links with lld
.现在cargo build
与lld
cargo build
链接。
This also works and is what I think @Jmb actually asked.这也有效,而且我认为@Jmb 实际上是这样问的。
rustflags = [
"-C", "linker=clang-12", # change the version as needed
]
Adding this to Cargo.toml
or .cargo/config.toml
(in the project) forces using lld将此添加到Cargo.toml
或.cargo/config.toml
(在项目中)使用 lld 强制
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.x86_64-pc-windows-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]
For more information see this: https://github.com/rust-lang/rust/issues/71515有关更多信息,请参见: https : //github.com/rust-lang/rust/issues/71515
You can remove "-C", "linker=clang"
on Linux, if you have Gcc 9 or newer installed如果您安装了 Gcc 9 或更新版本,您可以在 Linux 上删除"-C", "linker=clang"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.