Skip to main content

Rust

跨平台编译

打印目标构建平台

rustc --print=target-list

Linux musl

requires: musl-tools

  1. musl-tools
apt install musl-dev musl-tools
  1. build
cargo build --release --target=x86_64-unknown-linux-musl --verbose

Linux(x86_x64) -> aarch64

有些包似乎并没有 target_env,可能需要用 linux-gnu

requires: gcc-aarch64-linux-gnu, musl

  1. gcc-aarch64-linux-gnu
sudo apt install gcc-aarch64-linux-gnu
  1. build
cargo build --release --target aarch64-unknown-linux-musl

temporary linker:

export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc
export CC=aarch64-linux-gnu-gcc

or project cargo config:

# vim ~/.cargo/config.tmol
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
ar = "aarch64-linux-gnu-gcc"

ref: https://github.com/rust-lang/stacker/issues/80

Linux -> Windows

requires: mingw-w64
ref: https://www.reddit.com/r/rust/comments/5k8uab/crosscompiling_from_ubuntu_to_windows_with_rustup/

  1. mingw-w64
sudo apt update
sudo apt install mingw-w64
  1. add x86_64-pc-windows-gnu
rustup target add x86_64-pc-windows-gnu
  1. build
cargo build --release --target=x86_64-pc-windows-gnu --verbose

如果没有link: vim ~/.cargo/config.toml

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"