Install the Linux target with rustup and build your Rust project using the --target flag to cross-compile from macOS or Windows.
Use rustup to install the target toolchain and then compile with the --target flag.
- Install the target toolchain for your desired Linux architecture.
rustup target add x86_64-unknown-linux-gnu
- Compile your project for the Linux target using Cargo.
cargo build --release --target x86_64-unknown-linux-gnu
The resulting binary will be in target/x86_64-unknown-linux-gnu/release/.
Cross-compiling means building software on one operating system to run on another. You tell the Rust tool to download the specific rules for the target system and then build the code using those rules instead of your current computer's rules. It's like baking a cake using a recipe specifically designed for a different oven.