How to fix undefined reference to symbol 'dlclose@@GLIBC_2.2.5' from glad.c [duplicate]
Asked Answered
B

1

20

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.

Braggadocio answered 1/7, 2019 at 20:11 Comment(2)
DSO missing from command line is pretty obvious, you need to add -ldl. Though that linker call is really weird in the first place, what with passing absolute paths to libraries.Piloting
@Piloting 's comment should be an answer. That fixed my issueMccallum
F
40

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.

Flag answered 1/7, 2019 at 20:30 Comment(1)
Thanks. As you suggested, simply adding ${CMAKE_DL_LIBS} to target_link_libraries did the trick.Verdugo

© 2022 - 2024 — McMap. All rights reserved.