OpenGL,Visual C ++ 2012编译错误

时间:2013-10-02 13:59:15

标签: visual-c++ opengl

我正在尝试在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

3 个答案:

答案 0 :(得分:3)

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

您需要执行以下操作

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

答案 1 :(得分:1)

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

另请参阅documentation(但请记住,这是技术上为MS支持的OpenGL 1.1编写的,任何更新的功能都需要以其他方式提供,例如GLEW,GLUT等......)< / p>

答案 2 :(得分:0)

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