rustcompiler-errorsrust-cargo

error: linker `x86_64-w64-mingw32-gcc` not found


I am using MacOS Big Sur, and i am trying to cross compile to windows, but the problem is, this "error: linker x86_64-w64-mingw32-gcc not found" prevents me from doing that, here are my cargo dependencies:

[dependencies]
rand = "0.8.4"
macroquad = "0.3.13"
perlin_rust = "0.1.0"
libm = "0.2.2"

I have tried Cargo Clean/Update, and I have tried mvsc instead of gnu


Solution

  • TLDR;
    Besides installing a cross target with rustup you need to install an actual cross linker and tell cargo about it using cargo config file or an environment variable

    It seems you are attempting to cross compile your package. you can read here more about cross compilation; In a nutshell compiler is a program that takes your text source code and produces something the your operating system and cpu can understand.

    When you are building software for the platform you are developing on, it's all nice. You have all the tools but when you want to target another platform or os, you need a compiler that is produced to work on your machine but outputs a binary that is supposed to work on the target platform/os.

    So, In your case you need to install a cross toolchain that for mac for mingw target because rust does not have a cross linker itself. Once you get a cross toolchain all you need to do is to tell cargo how to find it.

    Here is a project aims to make cross compilation less painful.

    I also strongly advise you to read the cargo book config here you can see one of the ways of telling cargo about the cross linker another way is to use an environment variable (which I like better and easier to use with makefiles) and below you can see an example of that from one of my makefiles enter image description here and Again the cargo book refers to it enter image description here

    Overall cross compiling is painful it took me quite some time to understand the mechanics of it but it was worth it instead of copy pasting commands I found on the blogs. I also feel like it lacks severe documentation. Cargo book doesn't tell you anything about finding a linker assumes you know this already and pictures cross compiling as something just work out of box after installing a target toolchain with rustup.