Win32 example/tutorial with GC?

Johnny johnnykiro at
Tue May 1 13:14:23 PDT 2007

Hi Frits

I added the empty main() and the linking succeeds, however, the program 
does nothing since the entry point (main() )  returns immediately (which 
is expected.
So may be the good question is how to make GDC accept WinMain as an 
entry point for Windows GUI programs.

Strangely, I have another simpler GUI program that links correctly with 

import win32.winbase;
import win32.windef;
import win32.winuser;

extern(Windows) int WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         PSTR szCmdLine,
                         int iCmdShow)
       MessageBoxA(null, "Hello", "Hello Demo", MB_OK);
       return (0);

- Here is how I compile it:

	Administrator at home /cygdrive/c/D-Projects/gdc-test
	$ gdc -c gdctestwin.d -I"C:\dmd"

- Attempting to link with GDC fails:

	Administrator at home /cygdrive/c/D-Projects/gdc-test
	$ gdc -o gdctestwin.exe  gdctestwin.o
	/mingw/lib/libgphobos.a(cmain.o):(.text+0x19): undefined 	reference to 
	collect2: ld returned 1 exit status

- Linking with GCC sycceeds:

	Administrator at home /cygdrive/c/D-Projects/gdc-test
	$ gcc -o gdctestwin.exe  gdctestwin.o

- I tried to use GCC for linking my 1st example, but I get other errors:

	$ gcc -Wall -o WinMenu-gdc.exe WinMenu.o
	WinMenu.o:WinMenu.d:(.text+0xe7): undefined reference to 
	WinMenu.o:WinMenu.d:(.text+0xfb): undefined reference to 
	WinMenu.o:WinMenu.d:(.text+0x152): undefined reference to 
`CreateWindowA at 44'
	WinMenu.o:WinMenu.d:(.text+0x17e): undefined reference to `_d_assert'
	WinMenu.o:WinMenu.d:(.text+0x269): undefined reference to `LOWORD at 4'
	WinMenu.o:WinMenu.d:(.text+0x35a): undefined reference to 	`_Dmodule_ref'
	WinMenu.o:WinMenu.d:(.text+0x365): undefined reference to `_Dmodule_ref'
	WinMenu.o:WinMenu.d:(.data+0x48): undefined reference to 
	undefined reference to `_D13TypeInfo_Enum6__vtblZ'
	undefined reference to `_D10TypeInfo_i6__initZ'
	collect2: ld returned 1 exit status

- And here I added Phobos, but I get 1st error:

	$ gcc -Wall -o WinMenu-gdc.exe WinMenu.o -lgphobos
	/mingw/lib/libgphobos.a(cmain.o):(.text+0x19): undefined reference to 
	collect2: ld returned 1 exit status

So I'm confused :-( .. what are the definitive steps to compile and link 
a Windows GUI program using GDC/MinGW?


Frits van Bommel wrote:
> Johnny wrote:
>> It seems that the Phobos library itself refers to _Dmain and linker 
>> can't find it, so where is _Dmain, and why isn't it found?
> "_Dmain" is how the "main" of your program normally gets renamed 
> ("mangled") by the D compiler. It's not found because you didn't write 
> one :).
> What happens if you add an empty "void main() {}" ?
> I'm pretty sure the Windows version of DMD (or perhaps optlink) will 
> automatically use WinMain instead of main if it sees it, but I'm not 
> sure about GDC/MinGW. Maybe some special command-line option needs to be 
> passed?

More information about the D.gnu mailing list