The line:
/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2):
undefined reference to 'WinMain@16'
means that the linker has a reference within MinGW's main.o
which requires a WinMain
function to be provided by the user code. The most likely cause of this is that you haven't provided one.
When you create Win32 applications, main
is no longer the entry point you use. The entry point for your user code is now WinMain
so you need to provide that rather than main
:
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd);
The parameters are:
hInstance
, a handle to your applications instance, where an instance can be considered to be a single run of your application.
hPrevInstance
is always NULL.
lpCmdLine
, a pointer to a string that holding any command-line arguments.
nShowCMD
, which determines how your application's window will initially be displayed.
Now, having looked over your code and seen that you do actually have a WinMain
, it's most likely an environment isssue rather than a code issue.
Have you tried compiling with -mwindows
as an option?
A good start is to begin with the canonical beginner program and sort out the error there first. Enter the following program:
#include <windows.h>
int WinMain (HINSTANCE p1, HINSTANCE p2, LPSTR p3, int p4) {
MessageBox (0, "Hello.", "MyProg", MB_OK);
return 0;
}
and try to compile it the same way as your current program:
g++ -o qq.exe qq.cpp
Then try with:
g++ -mwindows -o qq.exe qq.cpp
and see if that works.
If it works in the former case, there's something wrong with your code. If not, there's probably something wrong with your compiler flags or environment.
And then, if adding -mwindows
works, it should solve your problem with your real code as well. And, if not, keep working with the simpler program since that has less variables that can cause problems.