错误:与“cc”链接失败:退出代码:1

49

我有一个.rs文件。当我通过rustc test1.rs编译它时,会出现错误:

    error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib' '-o' 'test1' 'test1.o' '-Wl,-force_load,/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a' '-Wl,-dead_strip' '-nodefaultlibs' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libcollections-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libunicode-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/librand-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libcore-4e7c5e5c.rlib' '-L' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib' '-L' '/Users/alex/Documents/projects/rust/.rust/lib/x86_64-apple-darwin' '-L' '/Users/alex/Documents/projects/rust/lib/x86_64-apple-darwin' '-lSystem' '-lpthread' '-lc' '-lm' '-lcompiler-rt'
note: ld: warning: directory not found for option '-L/Users/alex/Documents/projects/rust/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/alex/Documents/projects/rust/lib/x86_64-apple-darwin'
ld: can't open output file for writing: test1, errno=21 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error


$ rustc --version
rustc 1.0.0-dev

我看到一些与此相关的主题,但没有一个能帮我解决这个问题。


不知道为什么这个方法有效——我通过更新Rust解决了这个问题。 - Leo Ware
7个回答

72

在 Mac 上编译 Rust 时,我面临三个问题

第一个问题:如果你遇到了使用ld写文件/目录的问题,只需删除那些文件并尝试重新编译。我不知道为什么,在 Mac 上这个问题有时会发生。

第二个问题:如果你遇到其他ld错误(与文件访问无关):请尝试向~/.cargo/config添加以下部分(如果你没有此文件,请随意创建):

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

第三步:有时您的Mac缺少一些开发工具/依赖项。使用以下命令自动安装其中最重要的内容:

xcode-select --install

14
通常情况下,当您更新 Mac 操作系统至新的主要版本时,XCODE 工具链会过时,这是导致出现此错误的根本原因。重新安装最新版本的 Xcode 大多数时候可以解决这个问题。 - initvik
8
货物设置对我来说是遗漏的一部分,谢谢! - dcchuck
4
非常感谢 @denis。这节省了我大量时间。货物配置是关键。 - Steven
4
Cargo配置解决了我的错误,谢谢! - stereo
2
配置对我也起作用了! - tibbon
显示剩余6条评论

12
如果你遇到了note: /usr/bin/ld: cannot find -lsqlite3的问题, 那么请安装libsqlite3-dev: $ sudo apt install libsqlite3-dev
这个方法适用于Rust 1.53.0,Linux Mint 20.2(基于Ubuntu 20.04 LTS)

2
这不仅适用于sqlite3。 一般来说,如果您遇到编译错误以类似 **note** /usr/bin/ld cannot find -l<nameOfTheLibrary> 结尾,请参考此处的解决方案-https://dev59.com/eWQn5IYBdhLWcg3wmIHY。在我的情况下,我完全缺少了库,所以这个答案对我很有帮助-https://dev59.com/eWQn5IYBdhLWcg3wmIHY#42339395。 - nsrCodes
如果您在Linus或WSL2 Ubuntu上遇到此错误,这个方法可以解决。我在尝试使用Diesel安装SQLite时也遇到了这个错误。 - Kelvin Ukuejubola Oritsetimeyi

9

如果您有一台带有ARM处理器的MacBook M1(x),您需要从rustup安装Rust。https://sourabhbajaj.com/mac-setup/Rust/

当运行rustup-init时,请使用自定义选项将aarch64-apple-darwin更改为x86_64-apple-darwin

然后您可以将以下内容添加到.cargo/config.toml.cargo/config中(两个都可以)

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

此解决方案已测试通过 Rust 1.54 和 MacBook M1

我能够从本教程 https://www.youtube.com/watch?v=yqLD22sIYMo 进行 cargo build --release 并生成 dylib 文件。


这是在我的 M1 上为我工作的内容。 - program247365

7
我的 Rust 项目在更新了 MacOS 后无法编译,但这个命令解决了我的问题。
xcode-select --install

6
从您的命令 rustc test1.rs,编译器推断可执行文件的名称应为test1。链接器试图打开此文件以便写入可执行文件,但是失败了,错误代码是errno=21,其字符串版本为"Is a directory"。
这表明您的工作目录中有一个名为test1的目录导致冲突。

很准确。我曾经遇到过非常类似的问题,这个答案有助于缩小错误信息的范围。 - Alex

1

虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - Tyler2P

0
在我的情况下,我使用的是ubuntu22.04操作系统,遇到了以下错误。
error: linking with `cc` failed: exit status: 1
...
note: /usr/bin/ld: cannot find -lpython3.10: No such file or directory

我通过sudo apt install python3.10-dev解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接