Cross-compilation using Debian Rust, not rustup

I put some effort into the original rustc Debian packaging to make this "just work". So just to summarise the above thread into one place, here's an example of cross-compiling for aarch64-unknown-linux-gnu (using Debian "testing"):

Note the Debian name for this architecture is arm64. In practice, you would combine the various apt install invocations for a slight increase in overall speed - I've broken them out only for clarity.

dpkg --add-architecture arm64
apt update
# Important part here is "libstd-rust-dev:arm64"
apt install cargo rustc libstd-rust-dev:arm64

If you need to install something that uses gcc/g++ from build.rs then you'll need a C cross-toolchain

apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

Tell cargo to use the right linker when cross compiling. Note this is ~/.cargo/config, not in a specific project, and note cargo/rustc expect to link via gcc, not ld directly.

cat <<EOF >>~/.cargo/config
[target.aarch64-unknown-linux-gnu]
linker = "/usr/bin/aarch64-linux-gnu-gcc"
EOF

Note you might need extra native libraries, depending on crates in use. Eg, openssl-sys requires:

apt install libssl-dev:arm64
export PKG_CONFIG_ALLOW_CROSS=1

Ready!

cargo build --target aarch64-unknown-linux-gnu

file target/aarch64-unknown-linux-gnu/debug/mytest
target/aarch64-unknown-linux-gnu/debug/mytest: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=831ce6f2b01537416207ed67ed8ef09920144bbe, with debug_info, not stripped
  1. Profit.
4 Likes