Home >Backend Development >C++ >Why do I get an 'undefined reference to WinMain' error when using wWinMain in MinGW?
Mitigating Undefined Reference to WinMain when Employing wWinMain in C MinGW
When attempting to create a Windows application in C using MinGW (GCC), users may encounter an undefined reference to WinMain if they opt to utilize wWinMain instead of int main(). This error arises because MinGW's CRT startup library does not support wWinMain.
Solution:
Newer versions of MinGW provide the -municode linker option, enabling the switch to alternate startup code that permits the usage of wWinMain instead of WinMain. Incorporating this option into the command line, linker options in IDE, or makefile should resolve the issue:
g++ other_options_and_arguments -municode
However, it's worth noting that older MinGW versions lack support for this option. In such cases, consider using WinMain instead. In the provided example, since the program does not utilize the pCmdLine value, modifying wWinMain to WinMain and PWSTR pCmdLine to PSTR pCmdLine should suffice for compilation.
Unicode Command Line Handling:
If you later require Unicode command line handling, employ LPWSTR cmd_line = GetCommandLineW(); instead of WinMain argument.
The above is the detailed content of Why do I get an 'undefined reference to WinMain' error when using wWinMain in MinGW?. For more information, please follow other related articles on the PHP Chinese website!