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

アレッ*_*ックス 13 macos rust

我有一个单一的.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
Run Code Online (Sandbox Code Playgroud)

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

Rus*_*sso 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)

  • 这不仅仅是sqlite3特有的。一般来说,如果您遇到以“**note** /usr/bin/ld can't find -l<nameOfTheLibrary>”结尾的编译错误,请参阅此处的解决方案 - /sf/ask/1169703321/ /11565176 。就我而言,我完全错过了图书馆,所以这个答案帮助了我 - /sf/answers/2963757681/ (2认同)

Pat*_*Pat 10

如果您有配备 ARM 处理器的 MacBook M1(x),则需要从 rustup 安装 rust https://sourabhbajaj.com/mac-setup/Rust/

运行时rustup-init,使用自定义选项更改aarch64-apple-darwinx86_64-apple-darwin

然后你可以将以下内容添加到.cargo/config.toml.cargo/config(都可以)

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]
Run Code Online (Sandbox Code Playgroud)

该解决方案已使用 Rust 1.54 和 MacBook M1 进行了测试

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


syn*_*ned 10

我的 Rust 项目在更新我的 MacOS 后停止构建,所以这个命令为我修复了它

xcode-select --install
Run Code Online (Sandbox Code Playgroud)


DrY*_*Yap 6

根据您的命令,rustc test1.rs编译器推断可执行文件的名称应该是test1. 链接器尝试打开此文件,以便它可以写入可执行文件,但失败,errno=21其字符串化版本为“Is a directory”。

这表明您的工作目录中有一个名为的目录test1导致了冲突。


Den*_*din 6

我在 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",
]
Run Code Online (Sandbox Code Playgroud)

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

xcode-select --install
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢你@denis。这节省了我很多时间。货物配置是这里的交易 (4认同)
  • Cargo config解决了我的错误,谢谢! (4认同)
  • 每当您使用新的主要 mac 版本更新 Mac 时,XCODE 工具链通常会过时,这就是出现此错误的根本原因。大多数情况下,重新安装最新的 Xcode 可以解决该问题。 (2认同)
  • 货物配置对我来说是缺失的部分。谢谢你! (2认同)
  • 该配置也对我有用! (2认同)
  • 第三个选项解决了我的问题。我尝试了第一个选项,它显示了 xcode 的问题。然后尝试了3个选项 (2认同)