OpenGL GLut编译问题

时间:2011-03-19 22:44:33

标签: c++ visual-studio-2008 opengl

嗨我正在做一个openGL项目,但在下载并安装过剩库文件后,它仍然无法编译,我得到12个错误C3861:找不到标识符,任何帮助都会很棒

#include <windows.h>
#include <gl/gl.h>

void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);

int is_depth;

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(40, 40);
    glutCreateWindow("3D World");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutReshapeFunc(resize);

    glutMainLoop();

    return 0;

}

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    is_depth = 1;
    glMatrixMode(GL_MODELVIEW);

}

void display(void)
{
    if (is_depth)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    else
        glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
        glColor3f(0.2f, 0.2f, 0.2f);
        glVertex3f(-100.0, 0.0, -100.0);
        glColor3f(0.4f, 0.4f, 0.4f);
        glVertex3f(-100.0, 0.0, 100.0);
        glColor3f(0.6f, 0.6f, 0.6f);
        glVertex3f(100.0, 0.0, 100.0);
        glColor3f(0.8f, 0.8f, 0.8f);
            glVertex3f(100.0, 0.0, -100.0);     
    glEnd();

}

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'a':
        glTranslatef(5.0, 0.0, 0.0);
        break;

    case 'd':
        glTranslatef(-5.0, 0.0, 0.0);
        break;

    case 's':
        glTranslatef(0.0, 0.0, -5.0);
        break;

    case 'w':
        glTranslatef(0.0, 0.0, 5.0);
        break;

    }

    display();

}

void resize(int width, int height)
{
    if(height == 0) height = 1;

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, width / height, 1.0, 400.0);

    glTranslatef(0.0, -5.0, -150.0);

    glMatrixMode(GL_MODELVIEW);
}

我的错误是

1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(17) : error C3861: 'glutInit': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(18) : error C2065: 'GLUT_DOUBLE' : undeclared identifier
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(18) : error C2065: 'GLUT_RGB' : undeclared identifier
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(18) : error C3861: 'glutInitDisplayMode': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(19) : error C3861: 'glutInitWindowSize': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(20) : error C3861: 'glutInitWindowPosition': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(21) : error C3861: 'glutCreateWindow': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(23) : error C3861: 'glutDisplayFunc': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(24) : error C3861: 'glutKeyboardFunc': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(25) : error C3861: 'glutReshapeFunc': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(27) : error C3861: 'glutMainLoop': identifier not found
1>c:\users\riche\documents\visual studio 2008\projects\opengl\opengl\opengl.cpp(97) : error C3861: 'gluPerspective': identifier not found

glut32.dll文件在我的system32文件夹中

我的glut.h文件位于C:\ Program Files(x86)\ Microsoft Visual Studio 9.0 \ VC \ include

我的glut.lib文件位于C:\ Program Files(x86)\ Microsoft Visual Studio 9.0 \ VC \ lib

UPDATE 添加,现在我有这些错误

error LNK2019: unresolved external symbol _glutMainLoop@0 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutReshapeFunc@4 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutKeyboardFunc@4 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutDisplayFunc@4 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutCreateWindow@4 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutInitWindowPosition@8 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutInitWindowSize@8 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutInitDisplayMode@4 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol _glutInit@8 referenced in function _main
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode@4 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glEnable@4 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glClearColor@16 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glEnd@0 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glVertex3f@12 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glColor3f@12 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glBegin@4 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glTranslatef@12 referenced in function "void __cdecl keyboard(unsigned char,int,int)" (?keyboard@@YAXEHH@Z)
1>openGL.obj : error LNK2019: unresolved external symbol _gluPerspective@32 referenced in function "void __cdecl resize(int,int)" (?resize@@YAXHH@Z)
1>openGL.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity@0 referenced in function "void __cdecl resize(int,int)" (?resize@@YAXHH@Z)

4 个答案:

答案 0 :(得分:4)

此问题是由错误的.lib链接引起的。

在项目的适当位置,选择: 配置属性 - &gt;链接器 - &gt;输入

右栏中的

选择:附加依赖项, 将以下库添加到列表中,由分号分隔: opengl32.lib; glut32.lib; glu32.lib;

这将解决问题。

答案 1 :(得分:3)

仅使用#include <glut.h>

GLUT无论如何都包含所有gl.h函数。只需确保在项目设置中为glut32.lib,openGL32.lib等设置所有依赖项。

答案 2 :(得分:1)

您包含gl.h,但由于您使用的是GLUT(未正确包含在OpenGL中),因此您还需要#include <glut.h>

答案 3 :(得分:-1)

我创建了一个关于如何设置glfw的视频。这是20分钟的痛苦,但终于得到了它的工作。 http://www.youtube.com/watch?v=dpsC4kP2ME0 visual studio 2010似乎对配置非常敏感, 确保配置设置为“所有配置”。