货物:更改默认的rustc链接器

时间:2019-09-05 20:59:47

标签: rust rust-cargo lld

我想让rustc在特定的包装箱中使用lld作为链接器,而不是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板条箱中使用它。

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

2 个答案:

答案 0 :(得分:3)

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

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

现在cargo buildlld链接。

答案 1 :(得分:2)

这也有效,而且我认为@Jmb 实际上是这么问的。

rustflags = [
  "-C", "linker=clang-12",  # change the version as needed
]