CMake共享库具有多个可执行文件 [英] CMake share library with multiple executables
问题描述
我的项目包含几个共享一些常见代码的可执行文件。我想把公共代码放在静态库中,可执行文件可以链接到。 (通用代码非常小,我不喜欢处理共享库)。
My project contains several executables that share some common code. I would like to put the common code in a static library that the executables can link to. (The common code is pretty small and I prefer not to deal with shared libraries).
源代码树看起来像这样:
The source tree looks something like this:
- 项目
- CMakeLists.txt
- common
- CMakeLists.txt
- src
- 包括
- project
- CMakeLists.txt
- common
- CMakeLists.txt
- src
- include
- src
- CMakeLists.txt
- src
- CMakeLists.txt
app1和app2都依赖于共同的代码。
app1 and app2 both depend on the code in common.
这个公共代码是非常特定的应用程序,永远不需要被这个目录树之外的另一个项目使用。因此,我不想在任何类型的全局位置安装库。
This common code is very application specific and will never need to be used by another project outside this directory tree. For that reason I would prefer not to install the library in any sort of global location.
顶级CMakeLists.txt文件只是添加了子目录:
The top-level CMakeLists.txt file just adds the subdirectories:
project(toplevel) cmake_minimum_required(VERSION 3.1) add_subdirectory(common) add_subdirectory(app1) add_subdirectory(app2)
通用库的CMakeLists.txt文件创建静态库和集包括目录:
The common library's CMakeLists.txt file creates the static library and sets include directories:
add_library(common STATIC common.cpp) target_include_directories(common PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include")
可执行文件的文件如下所示:
And the file for the executables looks like this:
project(app1) cmake_minimum_required(VERSION 3.1) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} common)
现在我的问题。如果我从顶层项目目录运行CMake,我可以构建app1和app2,他们构建成功。但是,如果我想构建一个单独的一个这样的项目(通过运行CMake从app1,而不是从顶层目录构建),我得到一个错误,因为
common / include
未添加到标题搜索路径。Now for my question. If I run CMake from the top level project directory, I can build app1 and app2 and they build successfully. However, if I want to build a single one of these projects (by running CMake from app1, for example) instead of building from the top level directory, I get an error because
common/include
is not added to the header search path.我可以看到为什么会发生这种情况。对于app1或app2的CMakeLists.txt文件中没有什么拉入共同点。这只能在顶层进行。
I can see why this happens. There is nothing in the CMakeLists.txt file for app1 or app2 that "pulls in" common. This is only done at the top level.
有没有办法解决这个问题,或者这种行为通常被认为是可接受的?关于我的设置是不是最佳?我只是想,能够单独构建项目,而不是从顶级的事件中,我们开始开发越来越多的可执行文件使用这个公共库,这可能是很好的,但也许这是我不应该
Is there a way around this, or is this behavior generally considered acceptable? Is something about my setup sub-optimal? I'm just thinking it would be nice to be able to build the projects individually instead of from the top level in the event that we start to develop more and more executables that use this common library, but perhaps this is something I shouldn't be concerned about.
推荐答案
您应该使用
project()
子目录,前提是该子项目旨在作为独立和作为顶层项目的一部分构建。这是LLVM和Clang的情况,例如:Clang可以单独编译,但是当LLVM构建系统检测到Clang源时,它也包括它的目标。You should use
project()
command in subdirectories only if this subproject is intended to be built both as standalone and as a part of toplevel project. This is the case for LLVM and Clang, for example: Clang can be compiled separately, but when LLVM build system detects Clang source, it includes its targets too.case你不需要子项目。仅编译
app1
或app2
目标问题make app1
/make app2
在项目中构建dir。In your case you don't need subprojects. To compile only
app1
orapp2
target issuemake app1
/make app2
in projects build dir.这篇关于CMake共享库具有多个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!