使用CMAKE构建GLFW3应用程序 - GLFW_LIBRARIES没有设置

时间:2015-12-19 05:00:56

标签: cmake glfw pkg-config

我正在尝试使用glfw3构建一个小项目,但无论我做什么,我都无法通过pkgconfig来设置GLFW_LIBRARIES。

这是我的CMakeList.txt

cmake_minimum_required(VERSION 3.3)
project(LearnGLSL)

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

if (CMAKE_BUILD_TYPE STREQUAL "")
    set(CMAKE_BUILD_TYPE Debug)
endif()


if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/debug")
    set(PROJECT_BINARY_DIR "${CMAKE_SOURCE_DIR}/build/debug")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")


file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})


find_package(OpenGL REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW REQUIRED glfw3)



include_directories(
        ${OPENGL_INCLUDE_DIR}
        ${GLFW_INCLUDE_DIRS}
)


set(SOURCE_FILES main.cpp gl_core_4_3.cpp)

message(WARNING "${GLFW_LIBRARIES}")

add_executable(LearnGLSL ${SOURCE_FILES})
target_link_libraries(LearnGLSL ${OPENGL_gl_LIBRARY} ${GLFW_LIBRARIES})


add_custom_command(TARGET LearnGLSL  POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/assets
        ${PROJECT_BINARY_DIR}
        COMMENT "Copy resources to build tree")

这是安装glfw3的地方

-- Installing: /usr/local/include/GLFW
-- Installing: /usr/local/include/GLFW/glfw3native.h
-- Installing: /usr/local/include/GLFW/glfw3.h
-- Installing: /usr/local/lib/cmake/glfw/glfw3Config.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfw3ConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfwTargets.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfwTargets-noconfig.cmake
-- Installing: /usr/local/lib/pkgconfig/glfw3.pc
-- Installing: /usr/local/lib/libglfw3.a

我会第一个承认我对CMAKE不太满意,但这看起来很简单,我已经完成了我可以谷歌找到的所有内容。也许这是一个我没有注意到的错字。感谢任何帮助

哦,我忘了提到我在构建这个项目时得到了对glfw函数的未定义引用。我认为这是GLFW_LIBRARIES没有正确设置的结果。

1 个答案:

答案 0 :(得分:6)

我不知道如何使用pkgconfig查找GLFW但我认为在这种情况下你不需要pkgconfig。由于GLFW本身是使用CMake构建的,因此它应该安装一个本机CMake配置模块。

好吧,差不多。从v3.1.2开始,官方GLFW CMake配置模块支持有点麻烦。相反,请使用shaxbee's forkadasworks fork(基于shaxbee' s,但更新)

有了这个GLFW所有你需要找到的只有2行:

find_package(glfw3 REQUIRED)
...
target_link_libraries(LearnGLSL ... glfw)

我还在你的CMakeLists.txt中发现了一些其他问题,所以我重复整个脚本,修改过:

cmake_minimum_required(VERSION 3.3)
project(LearnGLSL)

set(CMAKE_CXX_STANDARD 11) # no explicit compiler flags if possible

# don't read CMAKE_BUILD_TYPE, it has no meaning with multiconfig
# generators
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/build/debug")

# PROJECT_BINARY_DIR should not be set at all
# You establish the BINARY_DIR with the initial cmake command
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

include_directories(${OPENGL_INCLUDE_DIR})

add_executable(LearnGLSL main.cpp gl_core_4_3.cpp)
target_link_libraries(LearnGLSL ${OPENGL_gl_LIBRARY} glfw)

add_custom_command(TARGET LearnGLSL POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_SOURCE_DIR}/assets
    ${PROJECT_BINARY_DIR}
    COMMENT "Copy resources to build tree")