我有一个单一的.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)
Pat*_*Pat 10
如果您有配备 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",
]
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)
根据您的命令,rustc test1.rs编译器推断可执行文件的名称应该是test1. 链接器尝试打开此文件,以便它可以写入可执行文件,但失败,errno=21其字符串化版本为“Is a directory”。
这表明您的工作目录中有一个名为的目录test1导致了冲突。
我在 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)