【问题标题】:Cross compile error `ld: unknown option: --as-needed`交叉编译错误`ld:未知选项:--as-needed`
【发布时间】:2022-08-23 20:11:41
【问题描述】:

在 Apple M1 中,当我为 Linux X86_64 编译 Rust 代码时,我收到错误 ld: unknown option: --as-needed

~/WORKDIR/rust/helloworld ⌚ 20:24:21
$ cargo build --target x86_64-unknown-linux-gnu
   Compiling helloworld v0.1.0 (/Users/fudenglong/WORKDIR/rust/helloworld)
error: linking with `cc` failed: exit status: 1
  |
  = note: \"cc\" \"-m64\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.19lk9s4zgix5szcj.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.1m3icqtseu62rr0o.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.2fy0lykh93934k3s.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.31qjfvz3m1ka31b5.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.3vrcx702oe0jhs7q.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.53gv3v3m2hy2kzin.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.cc5ryu0ilsy4zts.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.zedrxxva8ppoktz.rcgu.o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.2l2aexzp1dl763ew.rcgu.o\" \"-Wl,--as-needed\" \"-L\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps\" \"-L\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/debug/deps\" \"-L\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib\" \"-Wl,--start-group\" \"-Wl,-Bstatic\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-1b64d5fe7a3c3d7f.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-e7f86684b9679284.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-10a87791239bd676.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-0e6cad6dd623d38e.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-9d05dd05a4d51b33.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-a60ff138fe1adfad.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0e9344458c09713e.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-ef3b808d96f639bf.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-e3ce420901586b53.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-546bece8dd1bea42.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9b358af16d7d7aa1.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-554cd499fe1a3b52.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-d105877dbe329cd1.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-8fec8a74c706e4b4.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-e869a247063ed69a.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4896c4057dc9553a.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-2a6a2797f7a73818.rlib\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-0e3656b1fda5fd7b.rlib\" \"-Wl,--end-group\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-1f103368fa522bc0.rlib\" \"-Wl,-Bdynamic\" \"-lgcc_s\" \"-lutil\" \"-lrt\" \"-lpthread\" \"-lm\" \"-ldl\" \"-lc\" \"-Wl,--eh-frame-hdr\" \"-Wl,-znoexecstack\" \"-L\" \"/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib\" \"-o\" \"/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10\" \"-Wl,--gc-sections\" \"-pie\" \"-Wl,-zrelro,-znow\" \"-nodefaultlibs\"
  = note: clang: warning: argument unused during compilation: \'-pie\' [-Wunused-command-line-argument]
          ld: unknown option: --as-needed
          clang: error: linker command failed with exit code 1 (use -v to see invocation)


error: could not compile `helloworld` due to previous error

我已经安装了x86_64-unknown-linux-gnu 工具链。

标签: rust cross-compiling apple-m1


【解决方案1】:

您可能缺少正确的链接器。

安装 GNU 编译器集合:

brew install gcc

并将其添加到.cargo/config

[target.x86_64-unknown-linux-gnu]
linker = "/usr/local/bin/gcc-<version>"

【讨论】:

    【解决方案2】:
    $ brew tap SergioBenitez/osxct
    $ brew install x86_64-unknown-linux-gnu
    
    # .cargo/config.toml
    [build]
    target = "x86_64-unknown-linux-gnu"
    
    [target.x86_64-unknown-linux-gnu]
    linker = "/usr/local/bin/x86_64-unknown-linux-gnu-gcc"
    

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 2011-07-05
      • 2021-08-25
      • 2021-07-10
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      相关资源
      最近更新 更多