Rust 交叉编译 macOS 为 Linux 和 Windows

简介: Rust 交叉编译 macOS 为 Linux 和 Windows

前言

鉴于 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合集

目录
相关文章
|
1月前
|
人工智能 安全 Linux
Nexpose 8.2.0 for Linux & Windows - 漏洞扫描
Nexpose 8.2.0 for Linux & Windows - 漏洞扫描
68 20
Nexpose 8.2.0 for Linux & Windows - 漏洞扫描
|
7天前
|
安全 Ubuntu Linux
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
22 0
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
|
1月前
|
存储 安全 Linux
Metasploit Pro 4.22.7-2025040601 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025040601 (Linux, Windows) - 专业渗透测试框架
66 1
Metasploit Pro 4.22.7-2025040601 (Linux, Windows) - 专业渗透测试框架
|
14天前
|
存储 安全 Linux
Nexpose 8.4.1 for Linux & Windows - 领先的漏洞管理解决方案
Nexpose 8.4.1 for Linux & Windows - 领先的漏洞管理解决方案
34 0
Nexpose 8.4.1 for Linux & Windows - 领先的漏洞管理解决方案
|
1月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
48 0
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
|
Web App开发 存储 安全
Windows、macOS和Linux平台设置HTTPS以及自签名证书详细指南
本文讲的是Windows、macOS和Linux平台设置HTTPS以及自签名证书详细指南,在本文中,我会向大家介绍如何在Windows、macOS和Ubuntu Linux上设置使用HTTPS与ASP.NET Core的本地开发环境,另外,我还将介绍如何创建自签名证书并将其添加到受信任的根证书存储区,以消除烦人的浏览器提示消息。
3412 0
|
2月前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
121 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
|
2月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
113 4
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
|
27天前
|
Linux 虚拟化 iOS开发
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
74 29
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
|
2月前
|
存储 运维 监控
提升Windows Server环境安全性:ADAudit Plus的五大关键优势
在Windows Server环境中,内置的安全审计工具虽有用,但存在专业门槛高、耗时及功能缺失等问题。第三方工具ADAudit Plus应运而生,其五大优势包括:日志聚合、关键活动检测、定制化报告、灵活安全配置和长期日志保留,有效提升系统监控与合规能力。选择ADAudit Plus,助力企业更高效应对审计挑战,强化安全性。