How to compile .exe for windows with gcc from under linux?

Question:

Question: I wrote a simple C code for testing:

#include <stdio.h>
int main(void){
    printf("Translaaaaaatooooor\n");
    return 0;
}

And compiled it with gcc -c main.c , but an executable (Linux only) main.o was generated. If you run it ./main.o , it will display Translaaaaaatooooor.

My question is how can I compile main.c so that Windows can run it? Basically, how do you create an *.exe file with GCC on the Linux subsystem?

The executable created by gcc -o translaaaatoooor.exe main.c does not work on Windows.

Answer:

Moved from question

To create executable files for Windows under Linux, you need to install the mingw cross compiler:

 sudo apt-get install mingw-w64

You can then create a 32-bit Windows version of the .exe with:

 i686-w64-mingw32-gcc -o main32.exe main.c

And the 64-bit version of the .exe for Windows:

 x86_64-w64-mingw32-gcc -o main64.exe main.c

These Windows executables will not run inside the Linux subsystem, only outside of it.

Translation of @MarkusLaire answer

Scroll to Top