- 1 year ago
Cross-compiling Rust on Win10 for aarch64/Linux
Comment
1 / 1
To cross-compile Rust code on Windows 10 for the AArch64 architecture targeting Linux, you'll need to follow these general steps:
Install the Required Tools:
- Download and install the latest stable version of Rust from the official Rust website: https://www.rust-lang.org/tools/install
- Install the
aarch64-linux-gnu
toolchain. You can use the MinGW-w64 project to obtain the necessary toolchain binaries. You can download it from here: https://mingw-w64.org/doku.php/download/win-builds
Configure the Rust Toolchain:
- Open a Command Prompt or PowerShell window.
- Set the environment variables needed for cross-compiling. Replace
<path_to_toolchain>
with the path to the MinGW-w64 toolchain directory:
Note: Theshellset PATH=%PATH%;<path_to_toolchain>\bin
set CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
set CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER=qemu-aarch64-static
qemu-aarch64-static
runner is used for testing the compiled binary locally on Windows. It emulates the AArch64 architecture.
Create a New Rust Project or Modify an Existing Project:
- If you have an existing Rust project, open a Command Prompt or PowerShell window and navigate to its directory. Otherwise, create a new project by running the following command:shell
cargo new my_project_name
- Navigate to the project's directory:shell
cd my_project_name
- If you have an existing Rust project, open a Command Prompt or PowerShell window and navigate to its directory. Otherwise, create a new project by running the following command:
Configure Cargo for Cross-Compilation:
- Open the
Cargo.toml
file in a text editor and add the following lines at the end:toml[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
- Open the
Build and Test:
- Build your Rust project for the AArch64 target:shell
cargo build --target aarch64-unknown-linux-gnu
- If the build is successful, you can find the compiled binary in the
target/aarch64-unknown-linux-gnu/debug
ortarget/aarch64-unknown-linux-gnu/release
directory. - To test the compiled binary locally on Windows using the
qemu-aarch64-static
runner, run the following command:shellqemu-aarch64-static target/aarch64-unknown-linux-gnu/debug/my_project_name
- Build your Rust project for the AArch64 target:
Transfer and Run on a Linux AArch64 System:
- Copy the compiled binary to your Linux AArch64 system, either directly or via a network transfer method (e.g., SCP, SFTP).
- On the Linux system, make the binary executable if necessary:shell
chmod +x my_project_name
- Run the binary on the Linux system:shell
./my_project_name
By following these steps, you should be able to cross-compile your Rust code on Windows 10 for the AArch64 architecture targeting Linux and run it on a Linux AArch64 system.