When I try to build a program using Eclipse CDT
, I get the following:
/mingw/lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16
Why is that? And, how can I solve this issue?
When I try to build a program using Eclipse CDT
, I get the following:
/mingw/lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16
Why is that? And, how can I solve this issue?
To summarize the above post by Cheers and hth. - Alf, Make sure you have
main()
orWinMain()
defined and g++ should do the right thing.My problem was that
main()
was defined inside of a namespace by accident.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.
Try saving your .c file before building. I believe your computer is referencing a path to a file with no information inside of it.
--Had similar issue when building C projects
Consider the following Windows API-level program:
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: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:
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:
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:
However, with Microsoft's toolchain building as GUI subsystem does not work by default:
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-standardWinMain
instead of standardmain
.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 standardmain
: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 standardmain
. 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 standardmain
).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 formain
(no such), and for Microsoft's non-standardWinMain
(no such), reports that the latter is missing.Testing with an empty source: