Cross-Compiling with --target
Rust is a cross-compiler out of the box: rustc can generate code for any target LLVM supports. What you need besides the compiler is a standard library built for the target and a linker that can produce the target's executables. Cargo ties the three together with --target and a few lines of config. (Build Fundamentals: Cross-Compilation explains host, build and target triples.)
The three ingredients
| Ingredient | How you get it | On this course's machine |
|---|---|---|
| target std | rustup target add aarch64-unknown-linux-gnu |
installed |
| linker (and C toolchain for C code) | a cross GCC/Clang, or zig cc, or a container with both |
gcc-aarch64-linux-gnu 13.3 + arm64 libc |
| config | [target.<triple>] linker = "..." in .cargo/config.toml |
written in the session |
bash · try it yourself
rustup target list --installed
rustc --print target-list | grep -c .
rustc --print target-list | grep -E '^(aarch64-unknown-linux-gnu|x86_64-pc-windows-msvc|wasm32-unknown-unknown|thumbv7em-none-eabihf)