Cross compilation
You can cross compile your Rust code using the target specifier. For example:
cargo build --target x86_64-unknown-linux-gnu --release
You can list all targets using rustup target list and add the targets you need. But often you need an environment with the correct tooling. This is made easier using the cross (Github) tool.
Install cross
You can simply use cargo to install cross:
cargo install cross --git https://github.com/cross-rs/cross
Cross requires either docker or podman on your machine, because it uses docker images to provide the necessary tooling.
It automatically chooses the engine, but you can also set it using the environment variable CROSS_CONTAINER_ENGINE.
Build using cross
You can now build using cross by replacing the "cargo build" with "cross build":
# cross build --target <your target> <additional parameters>
cross build --target x86_64-unknown-linux-gnu --release
# Or with a different container engine
CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-unknown-linux-gnu --release