如何修复 glad.c 对符号 'dlclose@@GLIBC_2.2.5' 的未定义引用

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

我正在按照 https://learnopengl.com/ 上的教程学习 Opengl 我在使用 cmake(See Creating a window).

设置依赖项时遇到问题

我的 CMakeLists.txt 基于 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 成功构建了我的配置,但 ninja 无法编译并打印错误。

[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.

链接器抱怨找不到 dlclose。 您可以添加 libdlCMAKE_DL_LIBS。 此外,使用现代链接 targets 而不是 strings

将您的 CMakeLists.txt 更改为:

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})

查找 Generator Expressions 以了解 BUILD_INTERFACEINSTALL_INTERFACE