“Undefined reference to gluPerspective() when building GLUT project with CMake?”

huangapple go评论133阅读模式
英文:

Undefined reference to gluPerspective() when building GLUT project with CMake?

问题

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

英文:

I am currently working on rendering 3D objects using the GLUT utilities in OpenGL. I am doing this on a Linux operating system, and are using CMake to compile the code. Every time I go to compile the code I get an 'undefined reference' error, but only for particular functions within the glad library.

My include directory in my main.cpp looks like;

#include &lt;GL/glut.h&gt;

And in my CMakeLists.txt file I have;

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)

Everything looks good at least for what I need, but when I run the make command within my build file in the bash terminal. I get the error;

[ 33%] Linking CXX executable proj03
/usr/bin/ld: CMakeFiles/proj03.dir/main.cpp.o: in function `reshape(int, int)&#39;:
main.cpp:(.text+0x790): undefined reference to `gluPerspective&#39;
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

I've tried adding different libraries to see if the error would just go away that way, but it seems to only have this error for these particular functions within GLUT.

答案1

得分: 1

GLU 不等于 GLUT

FindOpenGL 定义了几个 IMPORTED 目标,包括 OpenGL::GLU。 将其添加到 target_link_libraries()

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

虽然如果你使用 GLAD,那么没有理由使用 OpenGL::OpenGL 目标,因为 GLAD 会在运行时通过 dlsym() 链接到系统的 OpenGL 共享库,并提供所有的 OpenGL 声明。 但你需要使用 include_directories() 来指定 CMake 到 GLAD 的 include 目录。 总结一下:

cmake_minimum-required(VERSION 3.17)

project(proj03)

find_package(OpenGL REQUIRED COMPONENTS GLU)
find_package(GLUT REQUIRED)

include_directories(SYSTEM "path/to/glad/include")

add_executable({PROJECT_NAME} main.cpp glad.c)
target_link_libraries(${PROJECT_NAME} OpenGL::GLU GLUT::GLUT)
英文:

GLU != GLUT.

FindOpenGL defines several IMPORTED targets, including OpenGL::GLU. Add that to target_link_libraries():

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

Though if you're using GLAD there's no reason to use the OpenGL::OpenGL target since GLAD handles linking against the system OpenGL shared library at run-time via dlsym() as well as providing all the OpenGL declarations itself. You will however have to use include_directories() to point CMake at the GLAD include directory. All together:

cmake_minimum-required(VERSION 3.17)

project(proj03)

find_package(OpenGL REQUIRED COMPONENTS GLU)
find_package(GLUT REQUIRED)

include_directories(SYSTEM &quot;path/to/glad/include&quot;)

add_executable({PROJECT_NAME} main.cpp glad.c)
target_link_libraries(${PROJECT_NAME} OpenGL::GLU GLUT::GLUT)

huangapple
  • 本文由 发表于 2023年3月4日 08:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定