glfw3的cmake package_search_module找到包,不提供完整的lib路径

时间:2016-08-28 03:35:47

标签: cmake homebrew glfw

这看起来非常简单,但我浪费了大量时间让它发挥作用。我只想构建和链接包含glfw3.h的main.cpp文件,但是cmake没有看到GLFW的lib路径。对package_search_module的调用是提供权限包括dir,但是生成的GLFW_LIBRARIES值只有lib名称'glfw3',并且它的dir没有在构建的命令行中给出。

我在这里缺少什么?

这是我非常简单的CMakeFiles.txt:

cmake_minimum_required(VERSION 3.3)
project(gltest)

set(CMAKE_VERBOSE_MAKEFILE true)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(gltest ${SOURCE_FILES})

find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(gltest ${GLFW_LIBRARIES})

message("GLFW_LIBRARIES=${GLFW_LIBRARIES}")

这是构建输出,在那里你可以看到GLFW_LIBRARIES包含'glfw3'的顶部:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug --target all -- -j 8
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/dave/dev/gltest -B/Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug --check-build-system CMakeFiles/Makefile.cmake 0
GLFW_LIBRARIES=glfw3
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug/CMakeFiles /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gltest.dir/build.make CMakeFiles/gltest.dir/depend
cd /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/dave/dev/gltest /Users/dave/dev/gltest /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug /Users/dave/Library/Caches/CLion12/cmake/generated/7a31a53e/7a31a53e/Debug/CMakeFiles/gltest.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gltest.dir/build.make CMakeFiles/gltest.dir/build
[ 50%] Linking CXX executable gltest
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/gltest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -std=c++11 -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/gltest.dir/main.cpp.o  -o gltest  -lglfw3 
ld: library not found for -lglfw3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [gltest] Error 1
make[1]: *** [CMakeFiles/gltest.dir/all] Error 2
make: *** [all] Error 2

最后,这里是用于pkg_search_module调用的glfw3.pc pkg-config:

prefix=/usr/local/Cellar/glfw3/3.2.1
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: GLFW
Description: A multi-platform library for OpenGL, window and input
Version: 3.2.1
URL: http://www.glfw.org/
Requires.private:
Libs: -L${libdir} -lglfw3
Libs.private:  -framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo
Cflags: -I${includedir}
~                                      

更新:我得到了它,但它仍然感觉不对 cmake docs建议不要使用link_directories,但是我可以通过使用它并建立一个链接库列表来实现这个目的:

set(LINK_LIBRARIES )

# GLFW lib
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
link_directories(${GLFW_LIBRARY_DIRS})
set(LINK_LIBRARIES ${LINK_LIBRARIES} ${GLFW_LIBRARIES})

add_executable(gltest ${SOURCE_FILES})
target_link_libraries(gltest ${LINK_LIBRARIES})

1 个答案:

答案 0 :(得分:0)

根据文档https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html <XPREFIX>_LDFLAGS(所有必需的链接器标志)和<XPREFIX>_LDFLAGS_OTHER(所有其他链接器标志)<XPREFIX> = GLFW >

相关问题