Rust Cross-Compilation Guide (Linux to Windows)

1. Install Rust Windows Target in Ubuntu (WSL/Linux)

To compile Rust programs on Ubuntu (or WSL) for Windows, install the Windows target:

rustup target add x86_64-pc-windows-gnu

2. Install MinGW for Cross Compilation

To generate Windows .exe files from Linux, install MinGW:

sudo apt update
sudo apt install mingw-w64 -y

3. Create a New Rust Project (Optional)

If you don’t already have a Rust project:

cargo new my_project
cd my_project

4. Modify Cargo.toml (Optional)

If your project uses Windows APIs, add the windows crate:

[dependencies]
windows = { version = "0.52", features = ["Win32_System_Memory", "Win32_System_Threading", "Win32_Foundation"] }

5. Compile for Windows

To compile a Rust project for Windows from Ubuntu:

cargo build --release --target x86_64-pc-windows-gnu

The Windows .exe file will be in:

target/x86_64-pc-windows-gnu/release/my_project.exe

6. Move the EXE to Windows

To copy the EXE to your Windows environment (if using WSL):

cp target/x86_64-pc-windows-gnu/release/my_project.exe /mnt/c/Users/YourUsername/Desktop/

Now you can execute it from Windows:

C:\Users\YourUsername\Desktop\my_project.exe

7. Running the EXE on Windows

From Windows CMD or PowerShell:

cd C:\Users\YourUsername\Desktop
.\my_project.exe

Summary

Step Command
Install Rust Windows Target rustup target add x86_64-pc-windows-gnu
Install MinGW sudo apt install mingw-w64 -y
Compile Rust for Windows cargo build --release --target x86_64-pc-windows-gnu
Copy EXE to Windows cp target/x86_64-pc-windows-gnu/release/my_project.exe /mnt/c/Users/YourUsername/Desktop/
Run EXE on Windows C:\Users\YourUsername\Desktop\my_project.exe