我目前正在使用 OpenGL 中的 GLUT 实用程序渲染 3D 对象。我在 Linux 操作系统上执行此操作,并使用 CMake 编译代码。每次我去编译代码时,我都会收到“未定义引用”错误,但仅限于 glad 库中的特定函数。
我的包含目录在我的
main.cpp
看起来像;
#include <GL/glut.h>
在我的 CMakeLists.txt 文件中;
cmake_minimum-required(VERSION 3.17)
project(proj03)
add_executable({PROJECT_NAME} main.cpp glad.c)
find_package(OpenGL REQUIRED COMPONENTS OpenGL)
find_package(GLUT REQUIRED)
add_dependencies(${PROJECT_NAME} OpenGL::OpenGL GLUT::GLUT)
target_link_libraries(${PROJECT_NAME} OpenGL::OpenGL GLUT::GLUT)
至少对于我需要的一切看起来都不错,但是当我在 bash 终端的构建文件中运行 make 命令时。我得到了错误;
[ 33%] Linking CXX executable proj03
/usr/bin/ld: CMakeFiles/proj03.dir/main.cpp.o: in function `reshape(int, int)':
main.cpp:(.text+0x790): undefined reference to `gluPerspective'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/proj03.dir/build.make:114: proj03] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/proj03.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
我尝试添加不同的库以查看错误是否会以这种方式消失,但似乎只有 GLUT 中的这些特定函数才会出现此错误。
FindOpenGL 定义了几个
IMPORTED
目标,包括 OpenGL::GLU
。将其添加到target_link_libraries()
:
target_link_libraries(${PROJECT_NAME} OpenGL::OpenGL OpenGL:GLU GLUT::GLUT)