Skip to content

Instantly share code, notes, and snippets.

@pich4ya
Last active March 6, 2023 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pich4ya/92a72f2c83a81948c5dc9ea6a498bf67 to your computer and use it in GitHub Desktop.
Save pich4ya/92a72f2c83a81948c5dc9ea6a498bf67 to your computer and use it in GitHub Desktop.
rust cross-compile for Windows (x86_64) and Linux (x86_64) on M1/M2
# @author Pichaya Morimoto (p.morimoto@sth.sh)
# The note at https://gist.github.com/shqld/256e2c4f4b97957fb0ec250cdc6dc463 is incomplete.
brew uninstall rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add x86_64-pc-windows-gnu
rustup target add x86_64-unknown-linux-gnu
rustup target list |grep installed
### Build Windows x86_64
brew install mingw-w64
cargo build --release --target x86_64-pc-windows-gnu
### Build Linux x86_64
brew tap messense/macos-cross-toolchains
brew install messense/macos-cross-toolchains/x86_64-unknown-linux-gnu
# consider adding theses to ~/.zshrc
export CC_X86_64_UNKNOWN_LINUX_GNU=x86_64-unknown-linux-gnu-gcc
export CXX_X86_64_UNKNOWN_LINUX_GNU=x86_64-unknown-linux-gnu-g++
export AR_X86_64_UNKNOWN_LINUX_GNU=x86_64-unknown-linux-gnu-ar
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc
cargo build --release --target x86_64-unknown-linux-gnu
### Output binaries
file target/x86_64-pc-windows-gnu/release/binary.exe
target/x86_64-pc-windows-gnu/release/binary.exe: PE32+ executable (console) x86-64, for MS Windows
file target/x86_64-unknown-linux-gnu/release/binary
target/x86_64-unknown-linux-gnu/release/binary: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.16, with debug_info, not stripped
# Please note that the binary is not stripped, you need a x86_64 Docker container to strip it.
$ strip binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment