【问题标题】:error: linking with `cc` failed: exit code: 1错误:与`cc`链接失败:退出代码:1
【发布时间】:2015-01-24 09:49:53
【问题描述】:

我有一个单个 .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

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

【问题讨论】:

    标签: macos rust


    【解决方案1】:

    我在 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
    

    【讨论】:

    • 每当您使用新的主要 Mac 版本更新 Mac 时,XCODE 工具链通常会过时,这是出现此错误的根本原因。大多数时候重新安装最新的 Xcode 可以解决问题。
    • 货物配置对我来说是缺失的部分。谢谢!
    • 非常感谢@denis。这为我节省了大量时间。货物配置是这里的交易
    • Cargo config 解决了我的错误,谢谢!
    • 该配置也对我有用!
    【解决方案2】:

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

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

    【讨论】:

    • 正确。我有一个非常相似的问题,这个答案有助于缩小错误消息的范围。
    【解决方案3】:

    如果你有“注意:/usr/bin/ld: 找不到 -lsqlite3”

    然后安装 libsqlite3-dev: $ sudo apt install libsqlite3-dev

    这适用于 Rust 1.53.0、Linux Mint 20.2(基于 Ubuntu 20.04 LTS)

    【讨论】:

    【解决方案4】:

    如果你有一台配备 ARM 处理器的 MacBook M1(x),你需要从 rustup https://sourabhbajaj.com/mac-setup/Rust/ 安装 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 进行了测试

    我能够做一个cargo build --release 并根据本教程生成一个dylib 文件https://www.youtube.com/watch?v=yqLD22sIYMo

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2017-04-12
      相关资源
      最近更新 更多