Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-compile a Rust application from Linux to Windows

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?

like image 219
Fedcomp Avatar asked Jul 18 '15 15:07

Fedcomp


People also ask

Can Rust be cross compiled?

These files run on Linux machines, and our goal is to cross compile them into a Windows executable. The cross compilation of your Rust code will be done via Docker. Download and install the latest version of Docker Desktop.

Does GCC support cross compilation?

GCC, a free software collection of compilers, can be set up to cross compile. It supports many platforms and languages. Cross-compiling GCC requires that a portion of the target platform's C standard library be available on the host platform.

Does Rust compile to exe?

Rust uses static linking to compile its programs, meaning that all libraries required by even the simplest Hello world! program will be compiled into your executable. This also includes the Rust runtime.


2 Answers

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 rustup toolchain install stable-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

like image 154
zee Avatar answered Sep 24 '22 14:09

zee


The Rust distribution only provides compiled libraries for the host system. However, according to Arch Linux's wiki page on Rust, you could copy the compiled libraries from the Windows packages in the download directory (note that there are i686 and x86-64 packages) in the appropriate place on your system (in /usr/lib/rustlib or /usr/local/lib/rustlib, depending on where Rust is installed), install mingw-w64-gcc and Wine and you should be able to cross-compile.

If you're using Cargo, you can tell Cargo where to look for ar and the linker by adding this to ~/.cargo/config (where $ARCH is the architecture you use):

[target.$ARCH-pc-windows-gnu] linker = "/usr/bin/$ARCH-w64-mingw32-gcc" ar = "/usr/$ARCH-w64-mingw32/bin/ar" 

Note: the exact paths can vary based on your distribution. Check the list of files for the mingw-w64 package(s) (GCC and binutils) in your distribution.

Then you can use Cargo like this:

$ # Build $ cargo build --release --target "$ARCH-pc-windows-gnu" $ # Run unit tests under wine $ cargo test --target "$ARCH-pc-windows-gnu" 
like image 23
Francis Gagné Avatar answered Sep 25 '22 14:09

Francis Gagné