Cross-compile

Cross-compile Rust from Windows — Linux, Wasm & ARM targets

Rust makes cross-compilation straightforward. Add a target with rustup target add, install a cross-linker, and build with cargo build --target. From Windows you can compile for Linux, macOS, ARM and WebAssembly.

Cross-compile from Windows to other platforms

PowerShell
# List available targets:
PS> rustup target list
# Add Linux musl target (static binary):
PS> rustup target add x86_64-unknown-linux-musl
# Add WebAssembly target:
PS> rustup target add wasm32-unknown-unknown
# Add ARM64 Linux target:
PS> rustup target add aarch64-unknown-linux-gnu

Compile Rust to WebAssembly from Windows

PowerShell
# Install wasm-pack for browser WebAssembly:
PS> cargo install wasm-pack
# Build Wasm package:
PS> wasm-pack build --target web
# Or build raw Wasm binary:
PS> cargo build --target wasm32-unknown-unknown --release

Cross-compile to Linux from Windows

Cross-compiling to Linux requires a Linux linker. The easiest approach on Windows is using the cross tool which uses Docker containers:

PowerShell
# Install cross (requires Docker Desktop):
PS> cargo install cross
# Cross-compile to Linux x86_64 musl:
PS> cross build --target x86_64-unknown-linux-musl --release
# Cross-compile to ARM64 Linux:
PS> cross build --target aarch64-unknown-linux-gnu --release

Cross-compile questions

Can I compile macOS binaries from Windows?

Not easily. macOS binaries require the macOS SDK which is not legally distributable. The practical solution is to use GitHub Actions, a macOS CI machine, or a macOS virtual machine to build macOS targets. The osxcross tool exists but requires manually obtaining the SDK.

How to compile Rust to .exe from Linux?

Add the Windows target on your Linux machine: rustup target add x86_64-pc-windows-gnu. Install MinGW: apt install gcc-mingw-w64-x86-64. Then build: cargo build --target x86_64-pc-windows-gnu --release. The result is a .exe that runs on Windows.