How to fix undefined reference to symbol 'dlclose@@GLIBC_2.2.5' from glad.c

SeaEyeHay picture SeaEyeHay · Jul 1, 2019 · Viewed 11.3k times · Source

I am learning Opengl by following the tutorial at https://learnopengl.com/ and I am having trouble setting up dependency with cmake(See Creating a window).

I based my CMakeLists.txt on the GLFW documentation.

cmake_minimum_required(VERSION 3.14)
project(openglTuto)


include_directories(include)
add_executable(gltuto src/main.c src/glad.c)

find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)

target_link_libraries(gltuto glfw)
target_include_directories(gltuto PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(gltuto ${OPENGL_gl_LIBRARY})

CMake succeed in building my configuration but ninja fail to compile and print an error.

[1/1] Linking C executable gltuto

FAILED: gltuto : && /usr/bin/cc CMakeFiles/gltuto.dir/src/main.c.o CMakeFiles/gltuto.dir/src/glad.c.o -o gltuto /usr/lib/libglfw.so.3.3 && :

/usr/bin/ld: CMakeFiles/gltuto.dir/src/glad.c.o: undefined reference to symbol 'dlclose@@GLIBC_2.2.5'

/usr/bin/ld: /usr/lib/libdl.so.2: error adding symbols: DSO missing from command line

collect2: error: ld returned 1 exit status

ninja: build stopped: subcommand failed.

Answer

kanstar picture kanstar · Jul 1, 2019

The linker is complaining about not finding dlclose. You can add libdl with CMAKE_DL_LIBS. Addtionally, make use of the modern linking with targets instead of strings.

Change your CMakeLists.txt to:

cmake_minimum_required(VERSION 3.14)
project(openglTuto)


add_executable(gltuto src/main.c src/glad.c)

find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)

target_include_directories(gltuto PUBLIC
                           $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
                           $<INSTALL_INTERFACE:include>)
target_link_libraries(gltuto PUBLIC glfw OpenGL::GL ${CMAKE_DL_LIBS})

Look up Generator Expressions to understand BUILD_INTERFACE and INSTALL_INTERFACE.