Learning Rust: Cross-compiler

Adrian Macal
2 min readJun 11, 2023

--

I develop with Rust on Linux and run it on Windows. How about you?

There are numerous reasons why you might choose to compile your application for multiple targets. In my scenario, I use Windows on a few of my machines, but I find Linux more suited for development. With Dev Containers, I avoid the need to install any toolchains or wrestle with multiple versions of Python. All installation processes are automated with Docker, and it works quite well.

However, there are instances when I need to run the application on the actual host and not on a virtual machine. This is primarily due to the performance degradation that occurs when mounting the host file system or networking.

Fortunately, setting up a cross-compiler in Rust is very straightforward. It essentially involves the following script, which installs the cross-compiler and the necessary Rust target:

sudo apt-get update
sudo apt-get install mingw-w64
rustup target add x86_64-pc-windows-gnu

To cross-compile your code, you need to specify the target:

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

The compiled file will then be accessible in the $CARGO_TARGET_DIR/x86_64-pc-windows-gnu/release directory. It will include .exe extension.

Cross-compiling is a amazing feature, particularly when you’re working with multiple operating systems simultaneously. Haven’t tried it yet? Give it a shot now!

--

--

Adrian Macal
Adrian Macal

Written by Adrian Macal

Software Developer, Data Engineer with solid knowledge of Business Intelligence. Passionate about programming.

No responses yet