问题
currently I am trying to make a windows application using c++. For compiling my program I use Mingw GCC. Btw I'm on Windows 10.
But as soon as I use int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
instead of int main()
the compiler shows me following message:
C:/mingw-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status The terminal process terminated with exit code: 1
The example code I tried to compile I got from this Windows application tutorial: Example Code
I have already tried reinstalling mingw but it did not help. That's why i hoped someone here could help me with my problem. Thank you in advance!
回答1:
This example code uses wWinMain
but
One thing to note is that Visual C++ supports a “wWinMain” entry point where the “lpCmdLine” parameter is a “LPWSTR”. You would typically use the “_tWinMain” preprocessor definition for your entry point and declare “LPTSTR lpCmdLine” so that you can easily support both ANSI and Unicode builds. However, the MinGW CRT startup library does not support wWinMain, so you’ll have to stick with the standard “WinMain” and use “GetCommandLine()” if you need to access command line arguments.
via Building Win32 GUI Applications with MinGW
Use WinMain
instead. This program doesn't use pCmdLine
value, so it should compile when you change wWinMain
to WinMain
and PWSTR pCmdLine
to PSTR pCmdLine
.
回答2:
undefined reference to `WinMain'
It tries to find WinMain
and failed. So you need use WinMain
instead of wWinMain
.
Another possible issue is
error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) ^~~~~~~ In file included from c:\mingw\include\windows.h:44:0, from test.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
So you need use LPSTR
instead of PWSTR
.
Then the entry point will like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
Above is ANSI version entry point.
回答3:
The solution is to add -municode
to the compilation parameters, as mentioned by @ssbssa in a comment.
Example:
g++ helloworld3.cpp -o helloworld3 -Wl,-subsystem,windows -municode
See https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/
While it is not necessary to define _UNICODE or UNICODE to compile the above code, -municode is needed for linking because it uses wmain() instead of the traditional main().
来源:https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw