Rust 交叉编译 macOS 为 Linux 和 Windows

打印 上一主题 下一主题

主题 876|帖子 876|积分 2628



目录

前言

鉴于 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 新建二进制项目:
  1. cargo new --bin hello
复制代码
文件main.rs:
  1. fn main() {
  2.     println!("Hello World!\n");
  3. }
复制代码
macOS 编译为 Linux 和 Windows 可用二进制程序

编译为 Linux 平台

想要实现Linux平台可以运行的程序,那么就必要使用musl来替代glibc,musl实现了Linux libc。
musl 在macOS上使用musl-cross,musl-cross是专门编译到Linux的工具链,下面进行安装:
musl https://musl.libc.org/
  1. $ brew install FiloSottile/musl-cross/musl-cross
复制代码
还必要创建musl-gcc:
  1. $ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc
复制代码
添加对应的Target,只必要执行一次就可以了:
  1. rustup target add x86_64-unknown-linux-musl
复制代码
修改配置文件~/.cargo/config(假如没有可以新建),添加以下内容:
  1. [target.x86_64-unknown-linux-musl]
  2. linker = "x86_64-linux-musl-gcc"
复制代码
也可以在项目根目录下创建 .cargo/config 文件,只对当前项目生效
  1. # 使用
  2. cargo build --release --target x86_64-unknown-linux-musl
复制代码
结果:
  1. $ tree -L 2 target/x86_64-unknown-linux-musl
  2. target/x86_64-unknown-linux-musl
  3. ├── CACHEDIR.TAG
  4. └── debug
  5.     ├── build
  6.     ├── deps
  7.     ├── examples
  8.     ├── hello
  9.     ├── hello.d
  10.     └── incremental
  11. 5 directories, 3 files
  12. $ file target/x86_64-unknown-linux-musl/debug/hello
  13. 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的工具链,使用如下命令进行安装:
  1. brew install mingw-w64
复制代码
添加接下来mingw-64的Target,只必要执行一次就可以了:
  1. $ rustup target add x86_64-pc-windows-gnu
复制代码
修改配置文件~/.cargo/config(假如没有可以新建),设置Linker,添加如下内容:
  1. [target.x86_64-pc-windows-gnu]
  2. linker = "x86_64-w64-mingw32-gcc"
  3. ar = "x86_64-w64-mingw32-gcc-ar"
复制代码
  1. # 使用
  2. $ cargo build --release --target x86_64-unknown-linux-musl
复制代码
结果:
  1. $ tree -L 2 target/x86_64-pc-windows-gnu
  2. target/x86_64-pc-windows-gnu
  3. ├── CACHEDIR.TAG
  4. └── debug
  5.     ├── build
  6.     ├── deps
  7.     ├── examples
  8.     ├── hello.d
  9.     ├── hello.exe
  10.     └── incremental
  11. 5 directories, 3 files
  12. $ file target/x86_64-pc-windows-gnu/debug/hello.exe
  13. target/x86_64-pc-windows-gnu/debug/hello.exe: PE32+ executable (console) x86-64, for MS Windows
复制代码
最后
  1. - https://tomshine.hashnode.dev/rust-macos-linux-windows
复制代码
rust合集

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曂沅仴駦

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表