Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an executable C file for Windows

Tags:

I know this question has been asked several times and I took a look at many of them like

  • Running linux gcc-compiled program under windows
  • How can I compile C files into an executable (.exe) file?

Unfortunately, none of them worked for me.

My situation

I've installed Ubuntu and Windows on my Notebook.

  • Let's say I developed a simple "Hello,World!"program using a text editor in c.
  • In Ubuntu, I've compiled it using GCC $ gcc -o hello.out -g -Wall -pedantic hello.c
  • I executed it './output.out'
  • And got the result Hello, World!

What I tried

So I kind of cross-developed here. I switched to Windows and kept going.

Now, I try to make it an executable file in order to run it on Windows. I know Windows can't handle '$ ./output.out' , alright, let's make it an executable then.

Under Windows, I've

  • installed cygwin
  • In Cygwin, I compiled it using GCC $ gcc -o hello.exe -g -Wall -pedantic hello.c

Note: I wrote hello.exe instead of hello.out or hello.c

  • In Cygwin, I executed it '$ ./output.exe'
  • And got the result Hello, World!

Note: At this point, it even works with my Shell under Windows because I installed Cygwin and set up my PATH etc. This means I can open my command line, go to the directory in which 'hello.exe' is located and execute it by typing '> hello.exe'

I thought that would be it, so I took hello.exe' and moved it to another notebook (not my local machine). I tried to execute it but it didn't work.

At first, I got an cygwin1.dll missing message. After fixing it, another error appears.

What I'm trying to accomplish

To make a long story short: The reason I wrote so much is that I want to give you a detailed look of my situation.

Basically, I'm trying to create an executable c file, which any Windows User could execute without having any development tools.

In Eclipse and Java, you could simply export your program making it a runnable -jar file. All the User has to do is install the latest Java SE version to get it running.

Additionally, I tried to compile my program in Visual Studio but that didn't work either.

Any suggestions? Thanks a lot!

like image 691
Big Dude Avatar asked Jul 12 '16 12:07

Big Dude


2 Answers

cygwin gcc produce an executable linked to the cygwin1.dll. So it is not usable without that.

gcc  hello.c -o hello-cygwin.exe

$ ldd hello-cygwin.exe
        ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
        kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77ab0000)
        KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdc60000)
        SYSFER.DLL => /cygdrive/c/Windows/System32/SYSFER.DLL (0x75650000)
        cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)

If you need a standalone program, a solution is to use the mingw compiler (it is available on cygwin as cross compiler to windows)

$ x86_64-w64-mingw32-gcc.exe hello.c -o hello-mingw64.exe

$ ldd hello-mingw64.exe
        ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
        kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77ab0000)
        KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdc60000)
        SYSFER.DLL => /cygdrive/c/Windows/System32/SYSFER.DLL (0x75650000)
        msvcrt.dll => /cygdrive/c/Windows/system32/msvcrt.dll (0x7fefdf40000)

You can move the resulting program on another windows machine that don't have cygwin installed.

like image 85
matzeri Avatar answered Sep 28 '22 03:09

matzeri


You should use mingw which is the gcc port for windows instead of gcc under cygwin. You can get it here.

like image 37
Ishay Peled Avatar answered Sep 28 '22 01:09

Ishay Peled