如何将正确的库与GLFW链接为cmake中的外部项目?

时间:2017-10-10 05:54:25

标签: c++ cmake glfw

我包括这样的glfw:

ExternalProject_Add(glfw-external
  URL https://github.com/glfw/glfw/releases/download/3.2.1/glfw-3.2.1.zip
  PREFIX ${CMAKE_CURRENT_BINARY_DIR}/glfw
  INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

add_dependencies(ShittyLife glfw-external)

if(UNIX)
  set(GLFW_LIB <What to put here?> libglfw3.a)
else()
  set(GLFW_LIB ${install_dir}/lib/glfw3.lib)
endif()

稍后使用GLFW_LIB变量链接库。

它在Windows上运行良好,但在Linux上我错过了一些库。 glfw网页提供了解决方案,但它们似乎都没有与ExternalProject_add的兼容性。

关于如何弄清楚所需库是什么的任何想法,最好是以不同机器/发行版的方式工作?

如果它在这里有帮助,那么我在尝试构建时会遇到错误:

相关位:

/usr/bin/ld: /home/molion/Desktop/ShittyLife/build/install/lib/libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line

全部:

[ 94%] Building CXX object CMakeFiles/ShittyLife.dir/src/main.cpp.o
[100%] Linking CXX executable ShittyLife
/usr/bin/ld: /home/molion/Desktop/ShittyLife/build/install/lib/libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/ShittyLife.dir/build.make:95: recipe for target 'ShittyLife' failed
make[2]: *** [ShittyLife] Error 1
CMakeFiles/Makefile2:68: recipe for target 'CMakeFiles/ShittyLife.dir/all' failed
make[1]: *** [CMakeFiles/ShittyLife.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

3 个答案:

答案 0 :(得分:5)

GLFW提供了一个CMake配置文件包,因此您应该使用find_package来查找它。

但是因为只有在ExternalProject_Add运行后才能使用,而不是在配置时,你必须创建一个超级项目并将你自己的项目添加为ExternalProject,如下所示:

ExternalProject_Add(glfw-test
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}
    INSTALL_COMMAND ""
    CMAKE_CACHE_ARGS "-DCMAKE_PREFIX_PATH:STRING=${GLFW_INSTALL_PREFIX}/lib/cmake"
    DEPENDS glfw-external
)

set(GLFW_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/glfw_install")GLFW_INSTALL_PREFIX也用于GLFW ExternalProject_Add

然后您可以使用

find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)

target_link_libraries(${PROJECT_NAME}
    glfw
    OpenGL::GL 
)

在你的项目中。

您也可以关注the GLFW suggestion,而不是使用ExternalProject

答案 1 :(得分:1)

如果您愿意使用包管理器来处理依赖关系,您可以考虑使用Hunter将GLFW添加到项目中。 Hunter是用本地CMake编写的,并且已经在引擎盖下使用了ExternalProject。然而,使用imo要容易得多。

有一个短(稀疏)documentation page。基本上你的CMakeLists.txt看起来像下面的......

# CMake version 3.0 or higher is required for the Hunter package manager.
cmake_minimum_required(VERSION 3.0)

# Include the Hunter package manager module.
include("cmake/HunterGate.cmake")

# Ensure a reproducible build by locking down the Hunter package versions.
HunterGate(
    URL "https://github.com/ruslo/hunter/archive/v0.19.77.tar.gz"
    SHA1 "8898dd07756c408c63c63add903c8600c4cf4c0e")

# Your project name here.
project(my_project_name)

# Tell Hunter which packages to pull in for our project.
hunter_add_package(glfw)
find_package(glfw3 REQUIRED)

# Link against your target.
target_link_libraries(my_target_name glfw)

每个新的Hunter release都可以找到神奇的HunterGate行。我最近发布了一个codecast on Hunter,如果你迷路了,它将帮助你完成这些步骤。

答案 2 :(得分:0)

这是我一直使用的CMakeLists.txt之一。很简单,你告诉CMake glfw3所在的位置,以便它的CMake可以完成它的工作,然后将glfw添加为链接库并给它包含目录。这与在视觉工作室中包含GLFW没什么不同。

cmake_minimum_required(VERSION 3.7)
project(SkyGames)

set(CMAKE_CXX_STANDARD 14)

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

###########################
# GLFW
###########################

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(${PROJECT_SOURCE_DIR}/Externals/GLFW3)

target_link_libraries(SkyGames glfw)
include_directories(SkyGames ${GLFW_INCLUDE_DIR})

###########################
# GLEW
###########################
add_definitions(-DGLEW_STATIC)
set(GLEW_ROOT_DIR ${PROJECT_SOURCE_DIR}/Externals/glew2s)
set(GLEW_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/Externals/glew2s/include)
set(GLEW_LIBRARY ${PROJECT_SOURCE_DIR}/Externals/glew2s/lib/Release/Win32/glew32s.lib)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIR})

##########################
# OPENGL
##########################

find_package(OpenGL REQUIRED)
target_link_libraries(SkyGames ${GLEW_LIBRARY} ${OPENGL_LIBRARY} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})