Compiling a very basic MinGW Windows Hello World executable in C with a Makefile


MinGW provides a complete Open Source programming tool set to compile native MS Windows applications, which do not depend on any non MS 3rd-party DLLs.

Here is a very minimal MinGW Hello World program in C.

DialogHello.c

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {

  MessageBox(NULL, "Hello World", "Hello", MB_OK);

  return 0;
}

Also provided is a generic Makefile which requires minimal changes from project to project. Simply change the value of the EXECUTABLE variable at the begining of the Makefile to determine the executable filename, when using it for new projects. Also add any extra required library flags to the LDFLAGS variable.

Makefile

EXECUTABLE=DialogHello.exe

CC="c:\MinGW\bin\gcc.exe"
LDFLAGS=-lgdi32

src = $(wildcard *.c)
obj = $(src:.c=.o)

all: myprog

myprog: $(obj)
	$(CC) -o $(EXECUTABLE) $^ $(LDFLAGS)

.PHONY: clean
clean:
	del $(obj) $(EXECUTABLE)

After installing MinGW, compile the program to make the Windows executable by placing the files “Makefile” and “DialogHello.c” in the same folder and type these commands at the command prompt in that folder:

"c:\MinGW\bin\mingw32-make.exe" all
DialogHello.exe

And now you have a windows executable you have compiled yourself without an expensive compiler.

dialoghello

Other Visual C and C++ programs can be made to compile with MinGW. It does support a lot of, but not all, the Visual C/C++ features.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s