调用gluPerspective和gluLookAt给了我一个不确定的参考

Calling gluPerspective and gluLookAt gives me an undefined reference?

本文关键字:一个 不确定 参考 gluPerspective gluLookAt 调用      更新时间:2023-10-16

我正在尝试以下来源,来自Instant Glew:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void initGraphics()
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    const float lightPos[4] = {1, .5, 1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0, 1.0, 1.0, 1.0);
}
void onResize(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(40, (float) w / h, 1, 100);
    glMatrixMode(GL_MODELVIEW);
}
void onDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0,
          0.0, 0.0, 1.0,
          0.0, 1.0, 0.0);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Teapot");
    initGraphics();
    glutDisplayFunc(onDisplay);
    glutReshapeFunc(onResize);
    glutMainLoop();
    return 0;
}

我的构建设置是Windows 10 VSCode,带有MSYS2,以及如下所示的Makefile

OBJS = 01-teapot.cpp
OBJ_NAME = C:/<"MyProjectPath">/build/01-teapot
INC_PATH = -IC:/msys64/mingw64/include/GL -LC:/msys64/mingw64/include/GL
INC_LINK_LIBS = -lglew32 -lopengl32 -lfreeglut
compiling :
    g++ -Wall $(OBJS) $(INC_PATH) $(INC_LINK_LIBS) -o $(OBJ_NAME) -g

但输出是这样的:

C:Users<"myUser">AppDataLocalTempcc5d3AtP.o: In function `onResize(int, int)':
c:<"MyProjectPath">PaPu_Instant_GLEW/01-teapot.cpp:22: undefined reference to `gluPerspective'
C:Users<"myUser">AppDataLocalTempcc5d3AtP.o: In function `onDisplay()':
c:<"MyProjectPath">PaPu_Instant_GLEW/01-teapot.cpp:31: undefined reference to `gluLookAt'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:32: compilando] Error 1

真的不知道我在哪里失败了。我忘了链接一些库吗?我已经尝试在链接的库上添加-lglut -lGLU,但编译器找不到它......

gluLookAt 是 GL Utility 库中的函数。您需要在链接器选项中包含-lglu32。这在将 GLUT 与 MinGW 一起使用中进行了解释。

你给库的顺序也很重要;请参阅我的相关答案以供参考。在您的情况下,请将glfw3替换为 freeglut

我相信您正在使用GLUT和GLU来学习OpenGL,这没关系,但请注意,它们当时是OpenGL 1的一部分,但不再是OpenGL不可或缺的一部分,并且已被弃用。 如果你正在做生产级的工作,我建议使用更成熟的库,如GLFW(而不是GLUT/FreeGLUT(;GLM具有GLU提供的所有便利功能。 请看达顿沃尔夫的回答。

相关文章: