Cross compilation on rust

To cross-compile is to build on one platform a binary that will run on another platform.

Host default target

When we type out cargo build, the compiler builds with host default target.
To check target manually, use cfg. The target triple is <arch>-<vendor>-<os>-<env>.

$ rustc --print cfg
debug_assertions
panic="unwind"
target_arch="x86_64"
target_endian="little"
target_env="gnu"
...
target_os="linux"
target_pointer_width="64"
target_thread_local
target_vendor="unknown"
unix

Also, we can check it using default-target crate.

$ cargo install default-target
$ default-target
x86_64-unknown-linux-gnu

Cross compilation

To cross compile, install target what we want to run on another platform.

rustup target add thumbv8m.main-none-eabi

Then, we can build like

cargo build --target thumbv8m.main-none-eabi

To set default target, write .cargo/config at crate root.

[build]
target = "thumbv8m.main-none-eabi"