我正在尝试迈出 OpenGL 的第一步。
但是它似乎不会发生,因为在尝试调试解决方案时出现此错误:
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol main referenced in function __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。
最佳答案
您有一个子系统 Console
的项目,而不是 Windows
。从您的项目属性中更改它,它将起作用。这是在 Linker -> System -> SubSystem 中。
关于c - 为什么我的 Windows 程序试图调用 main() 而不是 WinMain()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901825/