Rust 交叉编译 macOS 为 Linux 和 Windows

本文涉及的产品
函数计算FC,每月15万CU 3个月
性能测试 PTS,5000VUM额度
应用实时监控服务-应用监控,每月50GB免费额度
简介: rust 教程

d80ab0f7ea804a45a9863b2b57c5863d.jpeg

[toc]

前言

鉴于 rust 中文资料较少,遇到问题的解决方案更少。这里记录遇到的一些问题。

Rust 支持交叉编译,可以在 macOS 平台编译出 Linux 或者 Windows 可运行的程序,或者在 Linux 平台编译 macOS 或者 Windows 可运行的程序。

本文主要文章讲解Mac平台编译为其他平台的二进制程序。

想要实现跨平台编译且可运行的程序,那么我们就需要静态链接,这样生成程序才不会因为动态链接库的原因运行失败。

默认情况下,Rust 静态连接所有 Rust 代码。如果程序中使用了标准库,Rust 会连接到系统的libc实现。

环境

苹果系统:
操作系统:macOS 12.3.1 21E258 x86_64
生锈:rustc 1.60.0 (7737e0b5c 2022-04-04)
生锈:rustup 1.24.3 (ce5817a94 2021-05-31)

Linux:
操作系统:EndeavourOS Linux x86_64
核心:5.17.1-arch1-1
生锈:rustc 1.60.0 (7737e0b5c 2022-04-04)
生锈:rustup 1.24.3 (ce5817a94 2021-05-31)

首先需要安装Rust,使用命令`` 。

案例

使用 Cargo 新建二进制项目:

cargo new --bin hello

文件main.rs:

fn main() {
    println!("Hello World!\n");
}

macOS 编译为 Linux 和 Windows 可用二进制程序

编译为 Linux 平台

想要实现Linux平台可以运行的程序,那么就需要使用musl来替代glibc,musl实现了Linux libc。

musl 在macOS上使用musl-cross,musl-cross是专门编译到Linux的工具链,下面进行安装:

musl https://musl.libc.org/

$ brew install FiloSottile/musl-cross/musl-cross

还需要创建musl-gcc:

$ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

添加对应的Target,只需要执行一次就可以了:

rustup target add x86_64-unknown-linux-musl

修改配置文件~/.cargo/config(如果没有可以新建),添加以下内容:

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

也可以在项目根目录下创建 .cargo/config 文件,只对当前项目生效

# 使用
cargo build --release --target x86_64-unknown-linux-musl

结果:

$ tree -L 2 target/x86_64-unknown-linux-musl 
target/x86_64-unknown-linux-musl
├── CACHEDIR.TAG
└── debug
    ├── build
    ├── deps
    ├── examples
    ├── hello
    ├── hello.d
    └── incremental

5 directories, 3 files
$ file target/x86_64-unknown-linux-musl/debug/hello
target/x86_64-unknown-linux-musl/debug/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked, with debug_info, not stripped

编译为Windows平台

mingw-w64是用来编译到Windows的工具链,使用如下命令进行安装:

brew install mingw-w64

添加接下来mingw-64的Target,只需要执行一次就可以了:

$ rustup target add x86_64-pc-windows-gnu

修改配置文件~/.cargo/config(如果没有可以新建),设置Linker,添加如下内容:

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"

# 使用
$ cargo build --release --target x86_64-unknown-linux-musl

结果:

$ tree -L 2 target/x86_64-pc-windows-gnu
target/x86_64-pc-windows-gnu
├── CACHEDIR.TAG
└── debug
    ├── build
    ├── deps
    ├── examples
    ├── hello.d
    ├── hello.exe
    └── incremental

5 directories, 3 files
$ file target/x86_64-pc-windows-gnu/debug/hello.exe
target/x86_64-pc-windows-gnu/debug/hello.exe: PE32+ executable (console) x86-64, for MS Windows

最后

- https://tomshine.hashnode.dev/rust-macos-linux-windows

rust合集


目录
相关文章
|
6天前
|
Linux 虚拟化 Docker
Linux服务器部署docker windows
在当今软件开发中,Docker成为流行的虚拟化技术,支持在Linux服务器上运行Windows容器。流程包括:1) 安装Docker;2) 配置支持Windows容器;3) 获取Windows镜像;4) 运行Windows容器;5) 验证容器状态。通过这些步骤,你可以在Linux环境中顺利部署和管理Windows应用,提高开发和运维效率。
47 1
|
7天前
|
NoSQL IDE MongoDB
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
22 0
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
7天前
|
安全 Linux iOS开发
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
21 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
|
11天前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
20 0
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
|
14天前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
26 0
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
|
10月前
|
弹性计算 运维 安全
阿里云服务器Windows和Linux操作系统区别对比
阿里云服务器Windows和Linux操作系统区别对比,性能有差异吗?有,同配置下Linux性能要优于Windows,但这与阿里云无关,仅仅是linux和windows之间的区别。另外,阿里云提供的windows和linux操作系统均为正版授权,用户不需要额外支付许可费用,如何选择?看用户自己的应用程序情况。
|
10月前
|
运维 安全 Linux
使用阿里云搭建幻兽帕鲁服务器操作系统类型Windows和Linux有区别吗?
使用阿里云搭建幻兽帕鲁服务器操作系统类型Windows和Linux有区别吗?性能有差异?同配置的游戏服务器,如4核16G或8核32G配置等,选择Windows和Linux操作系统差异不太大,如果非要对比的话,Linux系统相对Windows更少占用计算资源,当然如果对Linux不熟悉的话,可以选择Windows。另外,即便是幻兽帕鲁服务器创建成功后,也是可以免费修改操作系统的
231 1
|
Linux 开发者 Windows
Windows、Linux 和 Mac:操作系统之间的区别
Windows系统、Linux系统与Mac系统:操作系统的对比与选择 操作系统是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行。操作系统是用户和计算机的接口,同时也是计算机硬件和其他软件的接口。以下是Windows 系统、Linux 系统、Mac 系统的对比: