Rust - Cross-compiling x86_64 for arm64
Basics
Add the target
rustup target add aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu
Make sure you specify the linker in .cargo/config.toml
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
Build for the specific target
cargo build --target aarch64-unknown-linux-gnu
cargo build --target aarch64-unknown-linux-gnu
Changing compliation targets, for e.g. WASM, use
rustup target add wasm32-unknown-unknown
rustup target add wasm32-unknown-unknown
Note that there are additional dependencies beyond the Rust toolchain (which provides the Rust standard library), such as a linker.
Cross compiling for any platform with Musl
Even if you can cross-compile, sometimes your run into GLIBC
version errors. In these cases, you can either change the machine you're compiling on, install the versions of GLIBC
required for your compilation (don't do this please), or compile with musl, which will statically link all required libraries into a standalone executable.
This can either be configured manually, or use one of the incredibly handy rust-musl-cross Docker images. For example,
docker pull messense/rust-musl-cross:aarch64-musl
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:aarch64-musl'
rust-musl-builder cargo build --release
docker pull messense/rust-musl-cross:aarch64-musl
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:aarch64-musl'
rust-musl-builder cargo build --release
Using cross-rs
Another alternative is to use cross-rs. Read the docs on this, and try running with a command like
cross build --target aarch64-unknown-linux-gnu
cross build --target aarch64-unknown-linux-gnu