具有多个可执行文件的 CMake 共享库 [英] CMake share library with multiple executables

查看:23
本文介绍了具有多个可执行文件的 CMake 共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目包含多个共享一些通用代码的可执行文件.我想将公共代码放在可执行文件可以链接到的静态库中.(通用代码很小,我不喜欢处理共享库).

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

源树看起来像这样:

  • 项目
    • CMakeLists.txt
    • 常见的
      • CMakeLists.txt
      • 源代码
      • 包括
      • 源代码
      • CMakeLists.txt
      • 源代码
      • 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 并且它们构建成功.但是,如果我想构建这些项目中的一个(例如,通过从 app1 运行 CMake)而不是从顶级目录构建,则会收到错误消息,因为未添加 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.

      就您而言,您不需要子项目.在项目构建目录中仅编译 app1app2 目标问题 make app1/make app2.

      In your case you don't need subprojects. To compile only app1 or app2 target issue make app1/make app2 in projects build dir.

      这篇关于具有多个可执行文件的 CMake 共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆