Rust 交叉编译 macOS 为 Linux 和 Windows

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 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合集


目录
相关文章
|
12天前
|
SQL 监控 安全
Linux&Windows 日志分析 陇剑杯 CTF
Linux&Windows 日志分析 陇剑杯 CTF
|
16天前
|
Linux Windows
Windows、Mac、Linux解决端口被占用的问题
Windows、Mac、Linux解决端口被占用的问题
24 1
|
23天前
|
安全 Ubuntu Linux
Linux远程访问Windows实现步骤
在Windows上启用远程桌面连接并获取IP地址后,Linux用户需安装SSH客户端( Debian系:`sudo apt-get update; sudo apt-get install openssh-client`,RPM系:`sudo yum install openssh-clients`)。然后使用命令`ssh 用户名@Windows_IP地址`连接,其中`用户名`和`Windows_IP地址`按实际情况填写。
16 4
|
1月前
|
Linux 数据安全/隐私保护 Docker
linux和windows中安装emqx消息服务器
linux和windows中安装emqx消息服务器
45 0
|
1月前
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
|
1月前
|
存储 安全 网络安全
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
|
1月前
|
网络协议 数据安全/隐私保护 Windows
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
47 1
|
1月前
|
存储 数据安全/隐私保护 索引
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
172 1
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
81 0
|
3天前
|
SQL 数据管理 关系型数据库
如何在 Windows 上安装 SQL Server,保姆级教程来了!
在Windows上安装SQL Server的详细步骤包括:从官方下载安装程序(如Developer版),选择自定义安装,指定安装位置(非C盘),接受许可条款,选中Microsoft更新,忽略警告,取消“适用于SQL Server的Azure”选项,仅勾选必要功能(不包括Analysis Services)并更改实例目录至非C盘,选择默认实例和Windows身份验证模式,添加当前用户,最后点击安装并等待完成。安装成功后关闭窗口。后续文章将介绍SSMS的安装。
7 0

热门文章

最新文章