我想让rustc使用lld作为链接器,而不是在特定的 crate 中使用ld.因此,我在我的项目目录中创建.cargo/config,如下所示:

[target.x86_64-unknown-linux-gnu]                                                                   
linker = "ld.lld"

这会导致链接器错误:

$ 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

rust-lld也一样.如果我设置linker = "ld"(这应该是默认值,对吗?),我只是

  = note: ld: cannot find -lgcc_s

我试图手动解析所有缺失的库(-C link-arg=--library-path=/usr/lib/x86_64-linux-gnu个之类),但这只会导致错误的链接和错误的二进制文件.

有趣的是,如果我将/usr/bin/ld替换为/usr/bin/ld.lld的符号链接,它的效果非常好(没有错误,从编译的二进制文件中我看到它确实与lld链接).然而,我不想让lld成为我的系统级链接器,我只想把它用在一个特殊的Rust 的 crate 中.

那么,更改默认rustc链接器的正确方法是什么?

推荐答案

感谢@Jmb comment,我找到了一个解决方案.事实证明,rustc使用的默认链接器实际上是cc(这很有意义——它提供了编译/链接C代码所需的所有默认值,这也适用于Rust).我们可以将一个参数传递给cc,使其与lld相关联:

[target.x86_64-unknown-linux-gnu]
rustflags = [
    "-C", "link-arg=-fuse-ld=lld",
]

现在有cargo build个链接和lld个链接.

Rust相关问答推荐

在‘await’点上使用‘std::同步::Mutex’是否总是会导致僵局?

如何在 struct 中填充缓冲区并同时显示它?

捕获Rust因C++异常而产生panic

是否可以为`T:Copy`执行`T. clone`的测试

在泛型 struct 的字段声明中访问关联的Conant

Rust TcpStream不能在读取后写入,但可以在不读取的情况下写入.为什么?

当两者都有效时,为什么Rust编译器建议添加';&;而不是';*';?

具有对同一类型的另一个实例的可变引用的

限制未使用的泛型导致编译错误

如果LET;使用布尔表达式链接(&Q);

函数内模块的父作用域的访问类型

用于判断整数块是否连续的SIMD算法.

如何创建一个可变的嵌套迭代器?

如果变量本身不是None,如何返回;如果没有,则返回None&Quot;?

什么时候使用FuturesOrdered?

为什么 GAT、生命周期和异步的这种组合需要 `T: 'static`?

decltype、dyn、impl traits,重构时如何声明函数的返回类型

当 T 不是副本时,为什么取消引用 Box 不会抱怨移出共享引用?

如何在 Rust 中编写修改 struct 的函数

在 Rust 中为泛型 struct 编写一次特征绑定