我正在尝试以下来源,来自 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;
}
我的build设置是 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
我真的不知道我哪里失败了。我忘记链接一些库了吗?我尝试在链接的库上添加 -lglut -lGLU
,但编译器找不到它......
最佳答案
gluLookAt
是 GL 实用程序库中的函数。您需要在链接器选项中包含 -lglu32
。 Using GLUT with MinGW 中对此进行了解释.
您提供库的顺序也很重要;请参阅my related answer以供引用。在您的情况下,请将 glfw3
替换为 freeglut
。
我相信您正在使用 GLUT 和 GLU 来学习 OpenGL,这没关系,但请注意,它们以前是 OpenGL 1 的一部分,但不再是 OpenGL 的组成部分,并且已被弃用。如果您正在进行生产级工作,我建议使用更成熟的库,例如 GLFW (而不是 GLUT/FreeGLUT); GLM具有 GLU 提供的所有便利功能。请参阅 datenwolf's answer 的末尾.
关于c++ - 调用 gluPerspective 和 gluLookAt 给我一个 undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764267/