Genert Orginaal
Genert Orginaal

Reputation: 301

Why is my Windows program trying to call main() instead of WinMain()?

I'm trying to make my first steps to OpenGL.

However it seems that it will not happen because of this error coming while trying to debug the solution:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup

I understand that the complier wants to see int main() ..., but doesn't it see WinMain call?

Here is the code:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    HWND hWnd;
} Glab_t;

static Glab_t glab;

char szClassName[ ] = "GLab";

static LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

    MSG messages;
    RECT rect;
    WNDCLASSEX wndClass;
    int screenWidth, screenHeight;
    int x, y, w, h;

    screenWidth = GetSystemMetrics(SM_CXSCREEN);
    screenHeight = GetSystemMetrics(SM_CYSCREEN);

    rect.left = (screenWidth - 582) / 2;
    rect.top = (screenHeight - 358) / 2;
    rect.right = rect.left + 582;
    rect.bottom = rect.top + 358;

    x = rect.left;
    y = rect.top;
    w = 640;
    h = 480;



    wndClass.hInstance = hInstance;
    wndClass.lpszClassName = szClassName;
    wndClass.lpfnWndProc = WindowProcedure;
    wndClass.style = CS_DBLCLKS;
    wndClass.cbSize = sizeof (WNDCLASSEX);
    wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wndClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndClass.lpszMenuName = NULL;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);

    if (!RegisterClassEx (&wndClass)) {
        return 0;
    }

    glab.hWnd = CreateWindowEx (
           0,
           szClassName,
           "GLab - OpenGL",
           WS_OVERLAPPEDWINDOW,
           x,
           y,
           w,
           h,
           HWND_DESKTOP,
           NULL,
           hInstance,
           NULL 
           );

    ShowWindow (glab.hWnd, nCmdShow);
    while (GetMessage (&messages, NULL, 0, 0)) {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return true;
}

I'm using MS Visual C++ 2010 Express.

Upvotes: 1

Views: 551

Answers (2)

Rivasa
Rivasa

Reputation: 6740

You have to change the properties for the project; a Console project will generally look for a main() function, whereas a Windows project looks for WinMain() instead.

Upvotes: 2

K-ballo
K-ballo

Reputation: 81349

You have a project of subsystem Console instead of Windows. Change it from your project properties, and it will work. That's in Linker -> System -> SubSystem.

Upvotes: 4

Related Questions