1

我正在尝试在 Visual Studio 2012 中编译我的 Win32、OpenGL 程序,但我不断收到此错误:

Error   1   error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _WinMain@16   C:\Users\Chief\Documents\Programming\C++\Projects\Practice\Practice\WinMain.obj Practice

Error   2   error LNK1120: 1 unresolved externals   C:\Users\Chief\Documents\Programming\C++\Projects\Practice\Debug\Practice.exe   1   1   Practice

这是我的代码:

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>

#include <gl/GL.h>

HWND hwnd;
int clientWidth = 800;
int clientHeight = 600;

bool InitMainWindow(HINSTANCE hInstance);
LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    if(!InitMainWindow(hInstance))
    {
        return 1;
    }
    MSG msg = {0};
    while(WM_QUIT != msg.message)
    {
        if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            //This is where all my updating and rendering stuff will go
        }
    }

    return static_cast<int>(msg.wParam);
}

bool InitMainWindow(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    ZeroMemory(&wcex, sizeof(WNDCLASSEX));
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = MsgProc;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszClassName = "Project2DClass";
    wcex.lpszMenuName = NULL;
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, "Failed to register window class", NULL, NULL);
        return false;
    }

    RECT r = { 0, 0, clientWidth, clientHeight };
    DWORD style = WS_OVERLAPPEDWINDOW;
    AdjustWindowRect(&r, style, false);
    int width = r.right - r.left;
    int height = r.bottom - r.top;
    int x = GetSystemMetrics(SM_CXSCREEN)/2 - width/2;
    int y = GetSystemMetrics(SM_CYSCREEN)/2 - height/2;

    hwnd = CreateWindow("Project2DClass", "Project 2D", style, x, y, width, height, NULL, NULL, hInstance, NULL);
    if(!hwnd)
    {
        MessageBox(NULL, "Failed to create window", NULL, NULL);
        return false;
    }

    ShowWindow(hwnd, SW_SHOW); 

    return true;
}

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

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

有人可以告诉我如何解决这个问题以及我做错了什么吗?我再次使用 microsoft visual studio 2012 和 openGL

4

3 回答 3

3

您在链接步骤中缺少一个或多个库:OpenGL32.lib

您需要执行以下操作

将“opengl32.lib”添加到项目属性->配置属性->链接器->输入->附加依赖项。

于 2013-10-02T14:03:39.733 回答
1

您需要链接到opengl32库,可能只是opengl32.lib.

另请参阅文档(但请记住,这在技术上是为 MS 支持的 OpenGL 1.1 编写的,任何更新的功能都需要以其他方式满足,例如 GLEW、GLUT 等...)

于 2013-10-02T14:03:33.707 回答
0

对于 1120 错误,我建议查看此链接:C++ Fatal Error LNK1120: 1 unresolved externals for 2019:http: //msdn.microsoft.com/en-us/library/799kze2z%28v=vs.80%29.aspx

于 2013-10-02T14:21:23.063 回答