使用 CMake 将 GLFW 链接到可执行文件的问题



我目前正在尝试将GLFW库与CLion中的可执行文件链接起来。经过几个小时的研究和反复试验,我被困住了。我正在使用find_package()来定位我的 GLFW 库。出于某种原因,我的 FindGLFW.cmake 文件在使用 find_library(( 时似乎在/usr/local/lib/libglfw.a中正确定位了库,但我的 CMakeLists.txt 返回空并带有 find_package((。我真的不明白这怎么可能。有人可以帮助我吗?

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(learnopengl)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
find_package(glfw 3.3 REQUIRED)
message(STATUS "(CMakeLists.txt) GLFW: [${glfw}]")
if (NOT glfw)
message(SEND_ERROR "(CMakeLists) Did not find glfw_library")
endif()

add_executable(learnopengl src/main.cpp)
target_link_libraries(learnopengl glfw)

FindGLFW.cmake

find_library( GLFW_LIBRARY
NAMES
glfw3 glfw
PATHS
${GLFW_LOCATION}/lib
$ENV{GLFW_LOCATION}/lib
/usr/lib64
/usr/lib
/usr/local/lib64
/usr/local/lib
NO_DEFAULT_PATH
DOC "The GLFW library"
)
message(STATUS "(FindGLFW.cmake) GLFW_INCLUDE_DIR: [${GLFW_INCLUDE_DIR}]")
message(STATUS "(FindGLFW.cmake) GLFW_LIB: [${GLFW_LIBRARY}]")
if (NOT GLFW_LIBRARY)
message(SEND_ERROR "(FindGLFW.cmake) Did not find glfw_library")

运行cmake ..时的输出

-- (FindGLFW.cmake) GLFW_INCLUDE_DIR: [/usr/local/include]
-- (FindGLFW.cmake) GLFW_LIB: [/usr/local/lib/libglfw3.a]
-- (CMakeLists.txt) GLFW: []
CMake Error at CMakeLists.txt:12 (message):
(CMakeLists) Did not find glfw_library

当我尝试直接将find_library((包含在CMakeLists.txt中时,它确实找到了库,但是当我尝试将其与可执行文件链接时,我收到链接器错误,指出它找不到库:

CMakeList.txt

cmake_minimum_required(VERSION 3.15)
project(learnopengl)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
find_library( GLFW_LIBRARY
NAMES
glfw3 glfw
PATHS
${GLFW_LOCATION}/lib
$ENV{GLFW_LOCATION}/lib
/usr/lib64
/usr/lib
/usr/local/lib64
/usr/local/lib
NO_DEFAULT_PATH
DOC "The GLFW library"
)
message(STATUS "(FindGLFW.cmake) GLFW_LIB: [${GLFW_LIBRARY}]")
if (NOT GLFW_LIBRARY)
message(SEND_ERROR "(CMakeLists.txt) Did not find glfw_library")
endif()
add_executable(learnopengl src/main.cpp)
target_link_libraries(learnopengl GLFW_LIBRARY)

输出:

~/dev/projects/opengl/learnopengl/build  cmake ..                                                                                                       
-- (CMakeLists.txt) GLFW_LIB: [/usr/local/lib/libglfw3.a]
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/Philipp/dev/projects/opengl/learnopengl/build
~/dev/projects/opengl/learnopengl/build  cmake --build .                                                                                                  
[ 50%] Linking CXX executable learnopengl
ld: library not found for -lGLFW_LIBRARY
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [learnopengl] Error 1
make[1]: *** [CMakeFiles/learnopengl.dir/all] Error 2
make: *** [all] Error 2

我真的很感激一些关于我做错了什么的提示。我不明白为什么它无法正确定位/链接库。

谢谢!

在 cmake 中,参数区分大小写,因此find_package应该是

find_package(GLFW 3.3 REQUIRED)

链接器问题可以在日志中轻松找到。您正在尝试链接 -lGLFW_LIBRARY,但是您要链接的是 GLFW_LIBRARY 的值/usr/local/lib/libglfw3.a 。

target_link_libraries(learnopengl ${GLFW_LIBRARY})

最新更新