简体   繁体   English

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

[英]Linking a static library to a shared library in cmake

I have been working on a fun project (a game engine) for awhile now and figured i could make it more portable and easy to compile across platforms if I use cmake. 我一直在从事一个有趣的项目(一个游戏引擎),有一段时间了,我发现如果我使用cmake,我可以使其更便携,更易于跨平台编译。 Right now i have it set up like so, with a main executable and then a bunch of shared libraries that the executable is linked to. 现在,我已经像这样设置了它,一个主要的可执行文件,然后是一堆链接到该可执行文件的共享库。 I've found all the material needed to produce the libraries and the executable, and linking those to the executable, but what of linking a dependency like a static library or another shared library to one of the libraries i produce? 我已经找到了生成库和可执行文件并将它们链接到可执行文件所需的所有材料,但是将诸如静态库或另一个共享库之类的依赖项链接到我生成的其中一个库又该怎么办呢? Here is a visual 这是视觉

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)

now i want all of these, the executable and the shared libraries built and put into the bin folder in the top level directory. 现在,我希望所有这些,可执行文件和共享库都已构建并放入顶层目录的bin文件夹中。 What i found suggested online for a setup like this was to make cmakelists.txt files in each folder, and then one in the root, for each project. 我发现在线建议进行这样的设置是在每个文件夹中创建cmakelists.txt文件,然后在每个项目的根目录中创建一个。 What i have thus far is this. 到目前为止,我所拥有的是这个。

#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)

in 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}")

in Sentiment_OGL4Renderer 在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)

in Sentiment_SFMLGui 在Sentiment_SFMLGui中

project(Sentiment_SFMLGui)

include_directories(${Sentiment_SOURCE_DIR})

add_library(Sentiment_SFMLGui SHARED Sentiment_SFMLGui.cpp)

in Sentiment_TestGame 在Sentiment_TestGame中

project(Sentiment_Game)

include_directories(${Sentiment_SOURCE_DIR})

add_library(Sentiment_Game SHARED Game.cpp)

As you can tell there are a lot of third party libraries, and i tried various methods of linking, like with target_link_libraries, and i cannot for the life of me figure how to link an external library to the ones i've made. 如您所知,有很多第三方库,我尝试了各种链接方法,例如使用target_link_libraries,而且我一生都无法确定如何将外部库链接到我制作的库。 First off, the renderer uses GLEW but it needs no external dependency so ignore that. 首先,渲染器使用GLEW,但是它不需要外部依赖,因此可以忽略它。 Then it needs OpenGL32.lib and Gdi32.lib from the windows sdk (only for windows). 然后,它需要Windows sdk中的OpenGL32.lib和Gdi32.lib(仅适用于Windows)。 As for SFML, i've got the DLL's in the bin folder which need to be linked, (can easily get the .so's when working in linux and can distribute with the final product if I ever choose to do so). 至于SFML,我在bin文件夹中有需要链接的DLL((在Linux中工作时可以很容易地获得.so,如果我选择这样做的话,可以随最终产品一起分发)。 I need these all linked as dependencies to the libraries i create, but nothing seems to work. 我需要将所有这些链接作为对我创建的库的依赖关系,但是似乎没有任何效果。 The project is all c++ and I am currently using mingw32 to compile it. 该项目都是c ++,我目前正在使用mingw32对其进行编译。 I'm brand new to cmake so please be gentle if it is really simple. 我是cmake的新手,所以如果真的很简单,请保持温柔。

To link external libraries, best practice is to use or create FindModule for given external library. 要链接外部库,最佳实践是为给定的外部库使用或创建FindModule

CMake comes with numerous modules that aid in finding various well-known libraries and packages. CMake带有许多模块,可帮助查找各种知名的库和软件包。

The list of standard modules is in the official documentation 标准模块列表在官方文档中

In case there is no standard module for your external library, you should write your own . 如果您的外部库没有标准模块,则应编写自己的

The OpenGL library has standard module FindOpenGL : 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM