I am using Linux/GNU GCC to compile C source code. Is there any way I can generate .exe
files for Windows running on x86 or x64 architecture? The compiled code needs to be generated on the Linux machine.

- 9,696
- 16
- 68
- 132

- 352
- 1
- 3
- 12
-
1The compiler is machine dependent. So I am guessing you will have to download windows version of gcc to properly compile your source code into a windows executable file. – John Doe Jun 21 '17 at 08:04
-
1You could try Wine, and it'll work fine. – ForceBru Jun 21 '17 at 08:04
-
[Is it possible to compile Windows binaries on a linux machine?](https://stackoverflow.com/q/1516690/995714), [Cross compile windows 64 bit .exe from linux](https://stackoverflow.com/q/2689813/995714) – phuclv Jun 21 '17 at 08:46
4 Answers
You would need a cross-compiler to create a Windows executable in Linux.
Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems.
Installing the cross-compilation
sudo apt-get install mingw-w64
32bit
i686-w64-mingw32-gcc -o test.exe test.c
64bit
x86_64-w64-mingw32-gcc -o test.exe test.c

- 2,146
- 19
- 22
-
Interesting. I've just naively tried this on one of my projects that uses `libcsv`, which I have installed on my Linux machine. However, attempting to compile it gives me: `fatal error: csv.h: No such file or directory` - would you have any insights or articles on how to deal with such issues when using Mingw-w64? – domsson Mar 29 '20 at 10:19
It is called cross-compiling. But GCC does not provide that functionality on its own. You can use the toolset provided by MinGW and/or MinGW-w64 projects. MinGW allows you to cross compile to Win32 platform and MinGW-w64 allows you to do the same thing for both Win32 and Win64. They are both based on GCC.

- 400
- 4
- 12
You need a cross compiler: A compiler that targets another system than it runs on.
A gcc
targeting windows comes in the mingw
package and can also be compiled to run on Linux, and many Linux distributions already have packages containing it, so just search your package management tools for "mingw".
Cross compilers have by convention the name of the system they target prepended to their name, so the variant of gcc
for compiling windows executables will probably be named something like i686-w64-mingw32-gcc
, but this might differ depending on the packages provided by your Linux distribution.
You need to use cross-compiller to compile for different OS. The most popular is mingw

- 802
- 8
- 17