Bhavya Gupta
Bhavya Gupta

Reputation: 186

Can not compile win32 C++ Using Mingw

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?

Upvotes: 1

Views: 402

Answers (1)

Szymon Bednorz
Szymon Bednorz

Reputation: 475

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.

Upvotes: 1

Related Questions