使用 CMake 构建 GLUT 项目时未定义对 gluPerspective() 的引用?

问题描述 投票:0回答:2

我目前正在使用 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 中的这些特定函数才会出现此错误。

c++ opengl cmake glut glu
2个回答
0
投票

GLU != GLUT.

FindOpenGL 定义了几个

IMPORTED
目标,包括
OpenGL::GLU
。将其添加到
target_link_libraries()

target_link_libraries(${PROJECT_NAME} OpenGL::OpenGL OpenGL:GLU GLUT::GLUT)

-1
投票

快速搜索指向 this 答案表明显然

gluPerspective
在 GLU 3.1 中已弃用。官方文档在here.

© www.soinside.com 2019 - 2024. All rights reserved.