15

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.

3

4 Answers 4

30

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?
2

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.

Comments

2

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.

Comments

1

You need to use cross-compiller to compile for different OS. The most popular is mingw

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.