OpenGL GLut编译问题

3

你好,我正在进行一个OpenGL项目,但在下载并安装了glut库文件后,它仍然无法编译,我收到了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。

更新 添加后,我现在遇到了这些错误。

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)

完整的逐步过程,以防其他人遇到相同的问题在这里: https://dev59.com/eF_Va4cB1Zd3GeqPVJ9W#8985903 - Jav_Rock
4个回答

4
这个问题是由于错误的.lib链接引起的。
在项目属性中,选择: 配置属性->链接器->输入
在右侧栏选择:附加依赖项, 将以下库添加到列表中,用分号隔开: opengl32.lib;glut32.lib;glu32.lib;
这将解决问题。

3

只需使用 #include <glut.h>

GLUT已经包含了所有的gl.h函数。只需确保在项目设置中设置了glut32.lib、openGL32.lib等所有依赖项。


1

你包含了gl.h,但由于你使用的是GLUT,它不包含在OpenGL中,因此你还需要#include <glut.h>


我认为在包含glut.h之前不应该包含gl.h,我不确定是glu还是glh有问题,但我记得其中一个不能这样做。 - deek0146
你已经修复了原始错误,现在你没有链接glut.lib。 - deek0146
1
你应该仔细了解编译和链接的过程。 - deek0146
@rich - 打开项目的属性,进入链接器/输入页面,在“附加依赖项”列表中添加glut.lib。 - Mormegil

-1
我制作了一个关于如何设置GLFW的视频。花费了20分钟的痛苦,但最终成功了。 http://www.youtube.com/watch?v=dpsC4kP2ME0 Visual Studio 2010似乎对配置非常敏感,请确保将配置设置为“所有配置”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接