Home >Backend Development >C++ >Why does MinGW throw an 'undefined reference to WinMain' error when using wWinMain in C ?

Why does MinGW throw an 'undefined reference to WinMain' error when using wWinMain in C ?

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 11:23:02300browse

Why does MinGW throw an

Resolving undefined reference to WinMain when using wWinMain in C (MinGW)

When attempting to use wWinMain (HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) as the program entry point in C under MinGW, an "undefined reference to WinMain" error can occur.

Cause:

MinGW, particularly older versions, does not natively support the wWinMain entry point, which expects a wide-character (WCHAR) command line argument.

Solution:

There are two potential solutions:

1. Use -municode Flag:

Newer versions of MinGW support the -municode linker flag, which switches to an alternate startup code that allows using wWinMain. Add this flag to your command line, linker options in your IDE, or Makefile:

g++ other_options_and_arguments -municode

2. Use WinMain Entry Point:

For older versions of MinGW or if you prefer not to use the -municode flag, you can revert to using the standard WinMain entry point. This requires modifying your code as follows:

  • Change wWinMain to WinMain
  • Change PWSTR pCmdLine to PSTR pCmdLine

Additional Note:

If you need to access Unicode command line arguments later in your program, use LPWSTR cmd_line = GetCommandLineW(); instead of using the WinMain argument.

The above is the detailed content of Why does MinGW throw an 'undefined reference to WinMain' error when using wWinMain in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn