check that all files are included in your project:

i had this same error pop up after i updated clion. after hours of tinkering, i noticed one of my files was not included in the project target. after i added it back to the active project, i stopped getting the undefined reference to winmain16, and the code compiled.

edit: it's also worthwhile to check the build settings within your ide.

(not sure if this error is related to having recently updated the ide - could be causal or simply correlative. feel free to comment with any insight on that factor!)


had same problem. to fix it i clicked save to save my .c file before building. i believe my computer was referencing a path to a file with no information inside of it.


my situation was that i did not have a main function.


try saving your .c file before building. i believe your computer is referencing a path to a file with no information inside of it.


i was encountering this error while compiling my application with sdl. this was caused by sdl defining it's own main function in sdl_main.h. to prevent sdl define the main function an sdl_main_handled macro has to be defined before the sdl.h header is included.


to summarize the above post by cheers and hth. - alf, make sure you have main() or winmain() defined and g++ should do the right thing.

my problem was that main() was defined inside of a namespace by accident.


this error occurs when the linker can't find winmain function, so it is probably missing. in your case, you are probably missing main too.

consider the following windows api-level program:

#define nominmax
#include <windows.h>

int main()
    messagebox( 0, "blah blah...", "my windows app!", mb_setforeground );

now let's build it using gnu toolchain (i.e. g++), no special options. here gnuc is just a batch file that i use for that. it only supplies options to make g++ more standard:

c:\test> gnuc x.cpp

c:\test> objdump -x a.exe | findstr /i "^subsystem"
subsystem               00000003        (windows cui)

c:\test> _

this means that the linker by default produced a console subsystem executable. the subsystem value in the file header tells windows what services the program requires. in this case, with console system, that the program requires a console window.

this also causes the command interpreter to wait for the program to complete.

now let's build it with gui subsystem, which just means that the program does not require a console window:

c:\test> gnuc x.cpp -mwindows

c:\test> objdump -x a.exe | findstr /i "^subsystem"
subsystem               00000002        (windows gui)

c:\test> _

hopefully that's ok so far, although the -mwindows flag is just semi-documented.

building without that semi-documented flag one would have to more specifically tell the linker which subsystem value one desires, and some windows api import libraries will then in general have to be specified explicitly:

c:\test> gnuc x.cpp -wl,-subsystem,windows

c:\test> objdump -x a.exe | findstr /i "^subsystem"
subsystem               00000002        (windows gui)

c:\test> _

that worked fine, with the gnu toolchain.

but what about the microsoft toolchain, i.e. visual c++?

well, building as a console subsystem executable works fine:

c:\test> msvc x.cpp user32.lib

c:\test> dumpbin /headers x.exe | find /i "subsystem" | find /i "windows"
               3 subsystem (windows cui)

c:\test> _

however, with microsoft's toolchain building as gui subsystem does not work by default:

c:\test> msvc x.cpp user32.lib /link /subsystem:windows
libcmt.lib(wincrt0.obj) : error lnk2019: unresolved external symbol _winmain@16 referenced in function ___tmaincrtstartu
x.exe : fatal error lnk1120: 1 unresolved externals

c:\test> _

technically this is because microsoft’s linker is non-standard by default for gui subsystem. by default, when the subsystem is gui, then microsoft's linker uses a runtime library entry point, the function where the machine code execution starts, called winmaincrtstartup, that calls microsoft's non-standard winmain instead of standard main.

no big deal to fix that, though.

all you have to do is to tell microsoft's linker which entry point to use, namely maincrtstartup, which calls standard main:

c:\test> msvc x.cpp user32.lib /link /subsystem:windows /entry:maincrtstartup

c:\test> dumpbin /headers x.exe | find /i "subsystem" | find /i "windows"
               2 subsystem (windows gui)

c:\test> _

no problem, but very tedious. and so arcane and hidden that most windows programmers, who mostly only use microsoft’s non-standard-by-default tools, do not even know about it, and mistakenly think that a windows gui subsystem program “must” have non-standard winmain instead of standard main. in passing, with c++0x microsoft will have a problem with this, since the compiler must then advertize whether it's free-standing or hosted (when hosted it must support standard main).

anyway, that's the reason why g++ can complain about winmain missing: it's a silly non-standard startup function that microsoft's tools require by default for gui subsystem programs.

but as you can see above, g++ has no problem with standard main even for a gui subsystem program.

so what could be the problem?

well, you are probably missing a main. and you probably have no (proper) winmain either! and then g++, after having searched for main (no such), and for microsoft's non-standard winmain (no such), reports that the latter is missing.

testing with an empty source:

c:\test> type nul >y.cpp

c:\test> gnuc y.cpp -mwindows
c:/program files/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined referen
ce to `winmain@16'
collect2: ld returned 1 exit status

c:\test> _

Related Query

More Query from same tag