将静态库链接到cmake中的共享库

时间:2014-02-05 07:13:50

标签: c++ cmake

我一直在研究一个有趣的项目(游戏引擎)已经有一段时间了,并认为如果我使用cmake,我可以使它更易于跨平台编译。现在我把它设置成这样,有一个主可执行文件,然后是一堆可执行文件链接到的共享库。我已经找到了生成库和可执行文件所需的所有材料,并将它们链接到可执行文件,但是将依赖项(如静态库或其他共享库)链接到我生成的库之一是什么?这是一个视觉

Sentiment      (name of project)
  -Root        (all the interfaces and components of the engine. main.cpp is here
  -Sentiment_OGL4Renderer    (the files for the Renderer library)
  -Sentiment_SFMLGui         (the files for the Gui library)
  -Sentiment_TestGame        (the code for a game)

现在我想要所有这些,可执行文件和共享库构建并放入顶级目录中的bin文件夹。我在网上建议的这样的设置是在每个文件夹中创建cmakelists.txt文件,然后在每个项目的根目录中创建一个文件。到目前为止我的是这个。

#Sentiment 
cmake_minimum_required(VERSION 2.6)
project(Sentiment)

set(RENDERER Sentiment_OGL4Renderer)
set(GUI Sentiment_SFMLGui)
set(GAME Test_Game)

add_definitions(-DBUILD_DLL)

list( APPEND CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")

set(EXECUTABLE_OUTPUT_PATH "${Sentiment_SOURCE_DIR}/bin")
set(LIBRARY_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}")
link_directories("${LIBRARY_OUTPUT_PATH}")

add_subdirectory("${RENDERER}")
add_subdirectory("${GUI}")
add_subdirectory("${GAME}")
add_subdirectory(Root)

在root

project(Sentiment_exe)

link_directories("${Sentiment_SOURCE_DIR}/bin")

AUX_SOURCE_DIRECTORY(. new_source_list)

add_executable("${CMAKE_PROJECT_NAME}" ${new_source_list})
target_link_libraries("${CMAKE_PROJECT_NAME}" "${LIBRARY_OUTPUT_PATH}/${RENDERER}" "${LIBRARY_OUPUT_PATH}/${GUI}" "${LIBRARY_OUTPUT_PATH}/${GAME}" "${ADDITIONAL_DEPENDENCIES}")
Sentiment_OGL4Renderer中的

project(Sentiment_OGL4-3Renderer)

include_directories(${Sentiment_SOURCE_DIR})
add_definitions(-DGLEW_STATIC)
add_library(Sentiment_OGL4-3Renderer SHARED Sentiment_OGL4Renderer.cpp GL/glew.cpp)
Sentiment_SFMLGui中的

project(Sentiment_SFMLGui)

include_directories(${Sentiment_SOURCE_DIR})

add_library(Sentiment_SFMLGui SHARED Sentiment_SFMLGui.cpp)

在Sentiment_TestGame

project(Sentiment_Game)

include_directories(${Sentiment_SOURCE_DIR})

add_library(Sentiment_Game SHARED Game.cpp)

正如你所知道的那样,有很多第三方库,我尝试了各种链接方法,比如target_link_libraries,我不能为我的生活想象如何将外部库链接到我做过的那些。首先,渲染器使用GLEW,但它不需要外部依赖,所以忽略它。然后它需要来自windows sdk的OpenGL32.lib和Gdi32.lib(仅适用于windows)。至于SFML,我在bin文件夹中有DLL需要链接,(在linux下工作时可以轻松获得.so,如果我选择这样做,可以分发最终产品)。我需要将这些作为依赖关系链接到我创建的库,但似乎没有任何工作。该项目全部是c ++,我目前正在使用mingw32来编译它。我是cmake的新手,所以如果真的很简单,请保持温柔。

1 个答案:

答案 0 :(得分:1)

要链接外部库,最佳做法是为给定的外部库使用或创建FindModule

  

CMake附带了许多模块,可帮助查找各种知名库和包。

标准模块列表为in the official documentation

如果外部库没有标准模块,您应该write your own

OpenGL库具有标准模块FindOpenGL

find_package (OpenGL)
if (OPENGL_FOUND)
  include_directories(${OPENGL_INCLUDE_DIR})
  target_link_libraries (Sentiment_OGL4-3Renderer ${OPENGL_gl_LIBRARY})
endif (OPENGL_FOUND)