Reputation: 1
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 <GL/glut.h>
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)':
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
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.
Upvotes: 0
Views: 652
Reputation: 52084
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 "path/to/glad/include")
add_executable({PROJECT_NAME} main.cpp glad.c)
target_link_libraries(${PROJECT_NAME} OpenGL::GLU GLUT::GLUT)
Upvotes: 1