- Install target mingw-w64:
brew install mingw-w64
- Add target to rustup:
rustup target add x86_64-pc-windows-gnu
- Create
.cargo/config
- Add the instructions below to
.cargo/config
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
- If it's not working try to run the following command:
cp -f /usr/local/Cellar/mingw-w64/7.0.0_1/toolchain-x86_64/x86_64-w64-mingw32/lib/{,dll}crt2.o `rustc --print sysroot`/lib/rustlib/x86_64-pc-windows-gnu/lib
- And finally, run:
cargo build --target=x86_64-pc-windows-gnu --verbose
rustup show
— shows targets amn toolchain
rustup target list
— shows all supported targets
A bit of theory:
To compile a source code for a platform different from your local, you need to specify a target. This tells the compiler which platform your code should be compiled for. For this reason, you need to install the appropriate GCC, the GNU Compiler Collection for Windows. Like this one: mingw-w64.
Then you should add the target to the Rust toolchain.
Toolchains are a set of linked tools that help the language produce a functional target code. They can provide extended functionality from either a simple compiler and linker program, or additional libraries
The next step is to add a linker. This can be set in the Cargo config
The Rust compiler sequentially goes through each source code file in your program and checks your code to make sure it follows the rules of the Rust language and translates your source code into a machine language file called an object file. After the compiler creates one or more object files, then another program called the linker take all the object files generated by the compiler and combine them into a single executable program. In addition to being able to link object files, the linker also is capable of linking library files. A library file is a collection of precompiled code that has been “packaged up” for reuse in other programs.
When the linker is added you can build a program.
Links: