Mikhail Mikhail - 3 months ago 37
C Question

Windows 10 Linux Subsystem Bash. How to Compile Executable Specially for Windows Platform with GCC in Linux [issue resolved]

Recently published a new update to the Windows 10 Anniversary Update includes Linux Subsystem based on Ubuntu. I install gcc and g++ with

sudo apt-get install gcc
and
sudo apt-get install g++
. Write simple C code for test:

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


And compile it with
gcc -c main.c
but in the directory in which I make a compilation created object file
main.o
(but this executable file has built only for Linux Subsystem). If I run it
./main.o
(in linux terminal) it displays the
Hello
. Alright.

My question. How can I compile Windows
*.exe
with GCC in Linux Subsystem ?

Answer

Linux Subsystem works as a Linux-computer. You can only run Linux executables inside it and default gcc creates Linux executables.

To create Windows executables, you need to install mingw cross-compiler:

sudo apt-get install mingw-w64

Then you can create 32-bit Windows executable with:

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

And 64-bit Windows executable with:

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

Note that these Windows executables will not work inside Linux Subsystem, only outside of it.

Comments