HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustCritical

Cross-compile a Rust application from Linux to Windows

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
crossfromcompilewindowslinuxrustapplication

Problem

Basically I'm trying to compile the simplest code to Windows while I am developing on Linux.

fn main() {
    println!("Hello, and bye.")
}


I found these commands by searching the internet:

rustc --target=i686-w64-mingw32-gcc  main.rs
rustc --target=i686_pc_windows_gnu -C linker=i686-w64-mingw32-gcc  main.rs


Sadly, none of them work. It gives me an error about the std crate missing

$ rustc --target=i686_pc_windows_gnu -C linker=i686-w64-mingw32-gcc  main.rs 

main.rs:1:1: 1:1 error: can't find crate for `std`
main.rs:1 fn main() {
          ^
error: aborting due to previous error


Is there a way to compile code on Linux that will run on Windows?

Solution

Other answers, while technically correct, are more difficult than they need to be. There's no need to use rustc (in fact it's discouraged, just use cargo), you only need rustup, cargo and your distribution's mingw-w64.

Add the target (you can also change this for whatever target you're cross compiling for):
rustup target add x86_64-pc-windows-gnu


You can build your crate easily with:
cargo build --target x86_64-pc-windows-gnu


No need for messing around with ~/.cargo/config or anything else.

EDIT: Just wanted to add that while you can use the above it can also sometimes be a headache. I wanted to add that the rust tools team also maintains a project called cross: https://github.com/rust-embedded/cross
This might be another solution that you want to look into

Context

Stack Overflow Q#31492799, score: 220

Revisions (0)

No revisions yet.