You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Linux交叉编译Rust程序到macOS平台?

Linux下Rust交叉编译到macOS的链接错误解决办法

你遇到的链接错误根源在于:交叉编译macOS目标时,Rust默认调用了系统自带的cc(GCC),而GCC不支持macOS特有的-arch编译选项,导致链接过程失败。

解决步骤

1. 安装macOS交叉编译工具链

在Linux上编译macOS程序需要Apple的Clang工具链,推荐使用osxcross来搭建:

  • 克隆osxcross仓库:
git clone https://github.com/tpoechtrager/osxcross.git
cd osxcross
  • 下载合法的macOS SDK包(例如MacOSX10.15.sdk.tar.xz),放到osxcross目录下的tarballs文件夹中
  • 编译并安装osxcross:
UNATTENDED=1 OSX_VERSION_MIN=10.15 ./build.sh
  • 将osxcross的可执行目录添加到系统PATH:
export PATH="$PATH:/path/to/osxcross/target/bin"

(替换/path/to/osxcross为你实际的osxcross安装路径)

2. 配置Rust使用macOS专用链接器

创建或编辑~/.cargo/config.toml文件,添加以下配置:

[target.x86_64-apple-darwin]
linker = "x86_64-apple-darwin19-clang"
ar = "x86_64-apple-darwin19-ar"

注意:darwin19对应你安装的macOS SDK版本(比如MacOSX10.15对应darwin19),请根据实际下载的SDK版本调整后缀。

3. 重新执行编译

现在使用rustc或cargo编译即可成功:

rustc ./main.rs --target=x86_64-apple-darwin
# 若使用Cargo项目,执行:
cargo build --target=x86_64-apple-darwin

内容的提问来源于stack exchange,提问作者Daniil Screpchenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:34:54