Building Mingw executables using Cygwin

Recently I wanted to compile a quick UDP test program on Windows and be able to ship it as a single stand-alone binary. I’ve compiled the source with GCC on Cygwin, and as you might already know, the executable is dependent on cygwin1.dll. This can be checked with the following command:

$ objdump -p udptest.exe | fgrep cygwin1.dll

It is possible to use the Cygwin development tools to build MinGW applications that do not depend on the Cygwin DLL and only depend on runtime libraries distributed as part of Windows.

The MinGW FAQ provides an answer on how use the Cygwin environment for development, yet generate non-Cygwin-dependant executables.

Adding the line

PATH=/cygdrive/c/MinGW/bin:$PATH

in /etc/profile before exporting PATH will have the effect that within the Cygwin environment the instance of GCC used is the MinGW version.

Note that the MinGW toolchain is gcc/g++ based but it’s using the WinAPI. If you want to use the UNIX Socket API by using the cygwin headers and libs, you have to supply the Cygwin DLL with your program. If this is not an option, you will need to rewrite specific parts of the project to conform with the winsock API.

Look here for some notes about Windows sockets versus Unix sockets. I did a rewrite by including the winsock2.h header file, like e.g.

#ifdef MINGW
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

I’ve built the executable with

gcc -DMINGW -o udptest udptest.c -lws2_32

and verified the dependencies with

$ objdump -p udptest.exe | fgrep DLL

The output

DLL Name: KERNEL32.dll
DLL Name: msvcrt.dll
DLL Name: WS2_32.DLL

shows that the executable is no longer dependent on the Cygwin DLL.

3 thoughts on “Building Mingw executables using Cygwin”

  1. Not sure when this happened, but looks like you don’t even need to install MinGW. Just install the MinGW compilers in the cygwin distribution, and use the aforementioned flag.

  2. Hey Dirk,
    This seems to be a coolest trick 😉 I tried like you said and and also installed the Mingw from the cygwin distribution, but notiong seems to be working, its still depending on the cygwin dlls ;(,

  3. It appears that the mingw in Cygwin can be used.

    $ i686-pc-mingw32-gcc.exe -o hello.exe hello.c

    $ objdump -p hello.exe | grep -i cygwin

Leave a Reply