调用 gluPerspective 和 gluLookAt 给我一个未定义的参考?

Calling gluPerspective and gluLookAt gives me an undefined reference?

我正在尝试以下来源,来自 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">\AppData\Local\Temp\cc5d3AtP.o: In function `onResize(int, int)':
c:\<"MyProjectPath">\PaPu_Instant_GLEW/01-teapot.cpp:22: undefined reference to `gluPerspective'
C:\Users\<"myUser">\AppData\Local\Temp\cc5d3AtP.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

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

gluLookAt 是 GL Utility 库中的一个函数。您需要在链接器选项中包含 -lglu32Using GLUT with MinGW.

中对此进行了解释

你给图书馆的顺序也很重要;请参阅 my related answer 以供参考。在您的情况下,将 glfw3 替换为 freeglut.

我相信您正在使用 GLUT 和 GLU 来学习 OpenGL,这没关系,但请注意,它们在过去是 OpenGL 1 的一部分,但不再是 OpenGL 的组成部分并且已被弃用。如果您正在做 production-level 工作,我建议您使用更成熟的库,例如 GLFW (instead of GLUT/FreeGLUT); GLM has all the convenience functions that GLU provides. See towards the end of datenwolf's answer