0

我正在尝试向 OpenGL 迈出第一步。

但是,由于尝试调试解决方案时出现此错误,它似乎不会发生:

MSVCRTD.lib(crtexe.obj):错误 LNK2019:函数 _ _tmainCRTStartup中引用的未解析的外部符号主

我知道编译器想看到int main() ...,但它没有看到 WinMain 调用吗?

这是代码:

#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;
}

我正在使用 MS Visual C++ 2010 Express。

4

2 回答 2

4

您有一个子系统项目,Console而不是Windows. 从您的项目属性中更改它,它将起作用。那在Linker -> System -> SubSystem中。

于 2012-06-05T17:21:09.030 回答
2

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.

于 2012-06-05T17:24:45.673 回答