Rust 在 MacOS 下如何交叉编译出 Ubuntu 的程序?

lihs
  • 43

关于 MacOS 交叉编译 Ubuntu 工具链的问题

通过 Homebrew 装过了 gcc,但是现在好像 ld 有点问题

环境信息

编译平台
MacOS Catalina (10.15)
brew install gcc 安装了最新的 gcc-9

目标平台
Ubuntu 16.04

主要出错信息

ld: unknown option: —as-needed

问题猜想

工具链的 ld 使用的是 Xcode 的,Linux 需要再找?

其他更多信息

Rust 交叉编译
cargo 1.36.0
rustc 1.36.0

操作流程

(Rust 安装流程省略)

添加目标平台

$ rustup target add x86_64-unknown-linux-gnu

初始化项目

$ cargo new hello-rust

添加 hello-rust/.cargo/config 文件

[target.x86_64-unknown-linux-gnu]
linker = "gcc-9"

执行以下命令

cargo build —target=x86_64-unknown-linux-gnu

--verbose 后的详细信息

   Compiling hello-rust v0.1.0 (/Users/lhs/Workspace/Rust/hello-rust)
     Running `rustc --edition=2018 --crate-name hello_rust src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=9724e7c55f7e558a -C extra-filename=-9724e7c55f7e558a --out-dir /Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -C linker=g++-9 -C incremental=/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/incremental -L dependency=/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps -L dependency=/Users/lhs/Workspace/Rust/hello-rust/target/debug/deps`
error: linking with `g++-9` failed: exit code: 1
  |
  = note: "g++-9" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.1hkht09m0p3vjlty.rcgu.o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.3738003umxo075yg.rcgu.o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.3zc82ay9ekshfq2j.rcgu.o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.4rpugv4747aj7ckt.rcgu.o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.vr6gnfcorpezul2.rcgu.o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.w23aaacs1khmbt5.rcgu.o" "-o" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps/hello_rust-9724e7c55f7e558a.y9nkzs6il137pd4.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps" "-L" "/Users/lhs/Workspace/Rust/hello-rust/target/debug/deps" "-L" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-9895e8982b0a79e7.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-57306b8ed3378a0e.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-54463ae5adae3649.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-d99667b199a9875a.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-51f4300e102199f5.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-b3ccf1f7737a6a91.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-157c37a3ceb78853.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-505bcee45aafb736.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-c29abd529ba4452b.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-895383a3232f6572.rlib" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-5594cb4f559bc761.rlib" "-Wl,--end-group" "/Users/lhs/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-38e90baf978bc428.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
  = note: ld: unknown option: --as-needed
          collect2: error: ld returned 1 exit status


error: aborting due to previous error

error: Could not compile `hello-rust`.

Caused by:
  process didn't exit successfully: `rustc --edition=2018 --crate-name hello_rust src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=9724e7c55f7e558a -C extra-filename=-9724e7c55f7e558a --out-dir /Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -C linker=g++-9 -C incremental=/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/incremental -L dependency=/Users/lhs/Workspace/Rust/hello-rust/target/x86_64-unknown-linux-gnu/debug/deps -L dependency=/Users/lhs/Workspace/Rust/hello-rust/target/debug/deps` (exit code: 1)
回复
阅读 2.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏