Can I cross compile my rust binary for windows on my Linux system?

Hello rustaceans !!

I'm newbie to rust , and I'm in love with this awesome ,blazing-fast langauge :).

My question is that can I make a windows binary in rust from my Linux system. I think it's called "cross-compilation" I guess. Sorry for the noob question.

Thanks y'all !

3 Likes

Yep! I've actually done this for work, getting GitLab's CI runner to compile windows binaries for me.

The easiest method is with cross. You can do cross-compilation without it, but you need a C compiler and linker that works for the target, the cross tool just automates this by using pre-generated docker images (this will require installing docker, sudo apt-get install docker).

Once you have docker installed and running, you can cross-compile just by doing

cross build --target x86_64-pc-windows-gnu

(the cross tool just wraps cargo to run the job inside the appropriate docker container)

9 Likes

@Michael-F-Bryan What is the advantage here in using docker versus running cargo directly with the right triple? Perhaps I should read more about Docker but I've never used it or have given it much attention so thought I would ask.

1 Like

You can definitely do it without docker, I know when I originally tried it, it was annoying because you need to install mingw on your system so the appropriate linker is available and you have access to windows-specific headers.

That was a while back now, so you may be able to get away with just adding the x86_64-pc-windows-gnu target via rustup and running cargo build --target $YOUR_TARGET.

2 Likes