Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile C code in Linux to run on Windows? [duplicate]

Tags:

c

linux

windows

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.

like image 337
Aditya Banerjee Avatar asked Jun 21 '17 08:06

Aditya Banerjee


People also ask

Can you compile in Linux and run on Windows?

However, there is thing called MinGW which uses basically the same concepts and includes a minimal GNU runtime so you can easily make programs that compile on a GNU/Linux system and run on Windows. Note, though, that you'll need to compile your code twice — C++ just isn't designed for "write once, run anywhere".

Does gcc compile for Windows?

GCC is portable and run in many operating platforms. GCC (and GNU Toolchain) is currently available on all Unixes. They are also ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is also a cross-compiler, for producing executables on different platform.


1 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
like image 132
skr Avatar answered Oct 11 '22 03:10

skr