79

Windows 10 Anniversary Update includes the Linux Subsystem for Ubuntu. I installed gcc with sudo apt-get install gcc.

I wrote some simple C code for testing purposes:

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

And compiled it with gcc -c main.c but the execute (Linux only) main.o is generated. If I run it ./main.o, it displays Hello.

My question is, how can I compile main.c so that Windows can run it? Basically, how do you generate a *.exe file with GCC in Linux Subsystem ?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Mikhail
  • 11,067
  • 7
  • 28
  • 53
  • 1
    `compile Windows exe`...aren't they already executable? – Sourav Ghosh Aug 05 '16 at 09:36
  • 2
    Your question is (to me at least) a bit unclear. Can you provide more information outlining why what you get is different from what you expected. Note also that by passing the `-c` option to the compiler you are explicitly telling it to perform the compilation step only -- and to *not* produce an executable by linking. – G.M. Aug 05 '16 at 09:52
  • @G.M. If I want to get the `* .exe` file should I just make so `gcc -o main.exe main.c` ? The fact is that when I do it and try to ran this output file `main.exe` I got this http://i.imgur.com/NUDCslM.jpg – Mikhail Aug 05 '16 at 09:56
  • Did you try to run it inside or outside of Linux Subsystem? – Markus Laire Aug 05 '16 at 09:57
  • @MarkusLaire I try to run it with double click on file – Mikhail Aug 05 '16 at 09:58
  • So. If even more specifically. I am wondering how to compile with gcc under a specific platform – Mikhail Aug 05 '16 at 09:58
  • Try to run it from command-line inside linux subsystem with `./main.exe`. If that works, then it seems that you might have linux executable, not windows. – Markus Laire Aug 05 '16 at 09:58
  • @MarkusLaire yes, it's work in linux subsystem. but how can I compile specifically for the windows platform? – Mikhail Aug 05 '16 at 10:01
  • 2
    Since this seems to be fully linux system, you would need a cross-compiler to compile windows executable in linux. – Markus Laire Aug 05 '16 at 10:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120220/discussion-between-markus-laire-and-mikhail). – Markus Laire Aug 05 '16 at 10:04
  • Use a cross-platform compiler – phuclv Aug 05 '16 at 11:21
  • Try `gcc -Wall -g main.c -o myprog` (this produces a Linux ELF executable) then run `./myprog` – Basile Starynkevitch Aug 05 '16 at 11:56
  • It seems to me that OP wants to use gcc in LSW as a kind of cross-compiler to output a Windows PE format executable rather than an ELF executable. That way you could run it from a Command Prompt and not just from LSW bash. It's an interesting question and should be doable. – hippietrail Nov 23 '19 at 18:54

2 Answers2

133

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.

Markus Laire
  • 2,837
  • 2
  • 17
  • 23
  • The man page for gcc seems to imply that there are some options for making Windows console and windowed exes. Is there any reason why they wouldn't work? -mconsole -mwindows – sam msft Jul 12 '17 at 00:31
  • 1
    I don't have much experience with creating Windows apps, so I don't know the details of those compiler options. Still those Windows-specific options will only work with *-mingw32-gcc compilers and not default gcc which is creating Linux executables. – Markus Laire Jul 12 '17 at 09:52
  • It seems like x86_64-w64-mingw32-gcc is changed such that it is designed to use the windows C runtime (MSVCRT.DLL) and other core Windows libraries instead of the standard C libs gcc links (presumably whatever is in /lib?). – sam msft Jul 14 '17 at 00:40
  • it seems to work but how can compile the file to .exe if the .c needs a .txt file as parameter – user12346352 Jan 06 '19 at 14:36
  • 1
    I doubt this is exactly correct. It says you "need to" use mingw. Surely that's only one option. GCC is famously supposed to be able to cross-compile between anything so surely it can also cross-compile x86-PE on an x86-ELF system? I don't know how to do it but it seems shocking if it's impossible. – hippietrail Nov 23 '19 at 18:57
  • Would there be any advantage to using mingw over cygwin? I was trying to avoid either. – Jackie Feb 24 '22 at 02:22
11

If you compile using gcc on linux it will produce an ELF file not a PE (what windows understand) file

To compile a program for windows inside linux you can use mingw.

david
  • 127
  • 1
  • 3