Rust 交叉编译 macOS 为 Linux 和 Windows

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 新建二进制项目:

bash 复制代码
cargo new --bin hello

文件main.rs

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 musl.libc.org/

bash 复制代码
$ brew install FiloSottile/musl-cross/musl-cross

还需要创建musl-gcc:

bash 复制代码
$ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

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

bash 复制代码
rustup target add x86_64-unknown-linux-musl

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

bash 复制代码
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

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

css 复制代码
# 使用
cargo build --release --target x86_64-unknown-linux-musl

结果:

bash 复制代码
$ 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,只需要执行一次就可以了:

csharp 复制代码
$ rustup target add x86_64-pc-windows-gnu

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

ini 复制代码
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"

css 复制代码
# 使用
$ cargo build --release --target x86_64-unknown-linux-musl

结果:

bash 复制代码
$ 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

最后

bash 复制代码
- https://tomshine.hashnode.dev/rust-macos-linux-windows

rust合集


相关推荐
PAK向日葵1 小时前
【算法导论】PDD 0817笔试题题解
算法·面试
uzong2 小时前
技术故障复盘模版
后端
GetcharZp3 小时前
基于 Dify + 通义千问的多模态大模型 搭建发票识别 Agent
后端·llm·agent
加班是不可能的,除非双倍日工资3 小时前
css预编译器实现星空背景图
前端·css·vue3
桦说编程3 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
IT毕设实战小研3 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
wyiyiyi4 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
gnip4 小时前
vite和webpack打包结构控制
前端·javascript
excel4 小时前
在二维 Canvas 中模拟三角形绕 X、Y 轴旋转
前端
阿华的代码王国5 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端