Rust交叉编译

Rust交叉编译

简介

Macos平台编译linux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## 1. 安装linux编译工具链
$ brew install FiloSottile/musl-cross/musl-cross

## 2. 创建musl-gcc
$ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

## 3. 添加linux平台目标
$ rustup target add x86_64-unknown-linux-musl

## 4. 修改配置文件
$ cat <<EOF >> ~/.cargo/config  
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
EOF

## 5. 编译
$ cargo build --release --target x86_64-unknown-linux-musl

MacOS平台编译windows

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## 1. 添加windows编译工具链
$ brew install mingw-w64

## 2. 添加windows平台目标
$ rustup target add x86_64-pc-windows-gnu

## 3. 修改配置文件
$ cat <<EOF >> ~/.cargo/config  
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"
EOF

## 4. 编译
$ cargo build --release --target x86_64-unknown-linux-musl

参考

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

  2. https://rustwiki.org/zh-CN/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html#musl-%E6%94%AF%E6%8C%81%E5%AE%8C%E5%85%A8%E9%9D%99%E6%80%81%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%96%87%E4%BB%B6

updatedupdated2024-05-102024-05-10