简体   繁体   English

如何更改默认的 rustc / Cargo 链接器?

[英]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 buildlld 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.

相关问题 如何将基于货物的项目的-L链接器标志传递给rustc? - How to pass -L linker flag to rustc for cargo based project? 如何将 rustc 标志传递给货物? - How to pass rustc flags to cargo? 我应该如何更新 Rustup 的版本? 不更新 rustc 或 cargo 的版本 - How should I update the version of Rustup? not updating the version of rustc or cargo 我们如何为同一台 linux 机器上的所有用户安装 rustc、cargo? - How do we install rustc, cargo for all the users in the same linux machine? 如何在现有项目中对Cargo进行类似的依赖解析,并且只使用rustc进行代码分析? - How can I get similar dependency resolution to Cargo in an existing project with only rustc for code analysis? 如何仅使用 cargo 或 rustc 为自定义目标编译 Rust 核心板条箱? - How can I compile the Rust core crate for a custom target using only cargo or rustc? 当我使用 rustc 编译时,为什么会出现在 Cargo.toml 中列为依赖项的“找不到板条箱”? - Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc? 如何更改`cargo install`放置二进制文件的位置? - How do I change where `cargo install` places binaries? 如何仅使用rustc而不使用商品链接动态Rust库? - How to link a dynamic Rust library using only rustc and not cargo? 带货的模具linker怎么用? - How to use the mold linker with cargo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM