How to fix "undefined reference to 'WinMain@16'" problem?

Jan 16, 2014 at 11:38pm
So, I am messing around on the Windows Dev Center (http://msdn.microsoft.com/en-us/library/windows(dont have to click that)) and I wanted to see how they say to create a window, and I tried it and the debugger says: undefined reference to WinMan@16.

What does that mean?!?!? Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#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, 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);
}
Jan 17, 2014 at 12:14am
You accidentally named your function wWinMain instead of WinMain, so your linker has no idea where to find the main entry point for your windows API application.
Jan 17, 2014 at 12:17am
Well, I just changed it and it still says it. I cant find any other places where it is wrong. Any other suggestions?
Jan 17, 2014 at 12:26am
PWSTR is also wrong... it's supposed to be LPSTR:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
Jan 17, 2014 at 12:42am
Actually, wWinMain (and the PWSTR thing too) work for unicode.
Jan 17, 2014 at 12:53am
But then would he have to configure his linker to look for the wide entry point?
Jan 17, 2014 at 1:08am
Alright, thanks everybody I fixed it. WinMain and LPSTR instead of wWinMain and etc. whatever, I will try and remember that for next time
Jan 17, 2014 at 4:52pm
Are you using MinGW, don't you ? If so, you should know that MinGW runtime does not implement wWinMain or wmain entry points.
Jan 17, 2014 at 10:41pm
YEaah I am. Where do I get MinGW Ru ntime? (or how...)
Jan 18, 2014 at 12:00am
If you have MinGW, you already have the runtime.

He was just saying that the wWinMain doesn't work with MinGW... which is why you had to use WinMain instead.
Jan 18, 2014 at 12:35am
Oh. Well that makes sense. I was wondering why they have two different compilers with the same name.... (basically same)
Jan 18, 2014 at 7:42pm
MinGW Runtime = The DLLs required for applications built with MinGW
MinGW = Tools to build applications made with MinGW (huh)

Same happens for any other compiler.

<x> Runtime = Requirements for running applications build with <x>
(Usually DLLs)
Topic archived. No new replies allowed.