1

I wanted to start learning win32 api but the g++ would always return with

undefined reference to `WinMain@16'

I copied my cpp code from microsoft's website from here - Link. I tried multiple other sources of "Hello World" but without luck. What should I do?

Bhavya Gupta
  • 186
  • 2
  • 12

1 Answers1

1

You can work with two types of entry-point functions in WinAPI: WinMain and wWinMain.

The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred.

Source: Microsoft Docs

Both should work well with Microsoft Visual C++ compiler. However, there are some problems with MinGW and wWinMain function.

Solution 1: Use Microsoft Visual C++ compiler.

Solution 2: Use MinGW and change your entry-point function to WinMain because it works correctly on MinGW (also change PWSTR pCmdLine to PSTR pCmdLine):

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR pCmdLine, int nCmdShow)
           ^^^^^^^                                 ^^^^

Solution 3: It is discussed here but I didn't test it. If you are using MinGW-w64, you can add
-municode as your command line option. Your original source code should be compiled correctly.

Szymon Bednorz
  • 425
  • 3
  • 10
  • 25
  • Solution 2 and 3 didn't work for me. WinMain function is already defined and it passes error. For solution 3, it worked on basic code but didn't work great. Solution 1 worked all good. – Bhavya Gupta Jul 06 '20 at 13:57