【问题标题】:linked library is valid in CMakeLists, but doesn't link at compile time链接库在 CMakeLists 中有效,但在编译时不链接
【发布时间】:2022-01-15 08:45:35
【问题描述】:

我刚开始使用 vulkan 和 GLFW,但是当我尝试编译测试程序时,它给了我一堆链接器错误:

/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)

find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})

add_executable(vulkan_test main.cpp loops.cpp)

target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})

知道为什么会这样吗?我安装了 vulkan 和 GLFW,libglfw.so 在我的/usr/lib/ 目录中,clangd 没有发现任何问题,但仍然无法正确链接。 是的,我已经尝试过谷歌搜索,但无济于事。

【问题讨论】:

  • 你确定变量GLFW_LIBRARIES实际上包含find_package(GLFW)的结果吗?根据您对glfw 的显式链接的解决方案,它没有。如何提取find_package(GLFW) 的结果的方式取决于您在目录/home/headass/CMake_Modules/ 中的FindGLFW.cmake 脚本。您也可以通过简单的message("GLFW_LIBRARIES: {GLFW_LIBRARIES}") 来检查变量的内容。
  • 我推荐使用 Premake (premake.github.io)。它比 CMake 好得多。

标签: c++ linux cmake glfw


【解决方案1】:

变化:

target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})

到:

target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})

完美运行

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2023-04-09
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2020-03-14
    • 2016-02-12
    相关资源
    最近更新 更多