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

发布于 2022-09-11 23:00:59 字数 5739 浏览 26 评论 0

关于 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文