Yes, you can cross-compile Rust programs (on nightly) from Windows to Linux using the linux-musl
target as long as none of the crates you use have any C dependencies / sources. You'll need to add this to your .cargo/config
:
[target.x86_64-unknown-linux-musl]
rustflags = ["-Z", "linker-flavor=ld.lld"]
You can then (after rustup target add x86_64-unknown-linux-musl
) build with cargo build --release --target x86_64-unknown-linux-musl
. This will produce static binaries which should run on any recent Linux variant.
I was even able to cross-compile a rocket hello-world this way some time ago, but for that I had to do a few extra steps because it contained a dependency using C sources that must be compiled with the CC crate. What I had to do was roughly this, if I remember correctly:
- Download the musl Linux sources and prepare its headers with make (need make for Windows from MinGW for this)
- Install LLVM, add
clang
to PATH - Cross-build with these extra environment variables:
CC_x86_64-unknown-linux-musl=clang
CFLAGS_x86_64_unknown_linux_musl=-I C:\tools\musl\musl-1.1.18\target\usr\local\musl\include