0

I'm trying to create a window using the Win32 API in C++, but I keep getting an error:

undefined reference to `WinMain@16'

When using the default build task in VSC, no executable is even created in the directory. I've already tried updating the %PATH% system variable to 'C:\MinGW\bin;C:\MinGW\msys\1.0\bin;'.

The code that I'm using is provided by Microsoft to produce a window, so I'm not sure what is causing the issue.

Here is my code:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);



            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • use [*/ENTRY:wWinMainCRTStartup*](https://learn.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol?view=msvc-160) – RbMm May 12 '21 at 18:34
  • I think this is the cause: `#define UNICODE` – drescherjm May 12 '21 at 18:37
  • You will not get an executable if the build failed. – drescherjm May 12 '21 at 18:40
  • Related: [https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/](https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/) – drescherjm May 12 '21 at 18:43
  • 4
    There are TONS of duplicate questions on StackOverflow for this exact error message, it is not even funny. Did you not try to search for existing answers before posting this question? – Remy Lebeau May 12 '21 at 18:47
  • @RemyLebeau It is easier to write a question than to search for an answer. It is less work to let other people fix the problem – rioV8 May 12 '21 at 20:53
  • @rio it's actually more work to write a question than to search for an existing one. Literally paste the error message into a search engine – David Heffernan May 12 '21 at 21:20
  • @DavidHeffernan Yes, but not from his point of view, fixing the problem/code – rioV8 May 12 '21 at 22:12
  • [https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw/58336414](https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw/58336414) – Daniel Sęk May 13 '21 at 05:49

0 Answers0