【问题标题】:Cmake with multiple directories具有多个目录的 Cmake
【发布时间】:2021-12-09 02:49:08
【问题描述】:
project
  |------ CMakeLists.txt (The main Cmake)
  |------ somePlace/someOtherPlace/CmakeLists.txt
  |          |----- some.proto (google proto files)
  |          |----- CMakeList.txt
  |------ Project2/CmakeListst.txt
             |----- .cpp files
             |----- .hpp files
             |----- CMakeList.txt  

我的拓扑与上面类似,我的主 cmake 可以生成 cmake 文件,在主 cmake 之后我可以构建 Project2:

make Project2

我已将 Project2 作为子目录添加到主 cmake 中。我这里没有问题。但是当我运行make Project2 时,我还想与Project2 一起构建common/someplace/CmakeLists.txt。我也知道我可以通过 cmake 和 make 命令在其目录中构建common/someplace/CmakeLists.txt。你可以查看somePlace/someOtherPlace/CmakeLists.txt

    INCLUDE(FindProtobuf)
    FIND_PACKAGE(Protobuf REQUIRED)
    INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
    PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER oamc_packets.proto)
    ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})

但是我希望 Project2 依赖 somePlace/someOtherPlace/CmakeList.txt 并与之一起构建。我该如何实现呢?

我应该使用add_executable/add_library 命令吗?问题是somePlace/someOtherPlace/CmakeLists.txt 创建了头文件和 cc 文件以及 .a 文件。

PS:如果需要,我可以提供更多信息。

【问题讨论】:

  • 添加common/someplace/作为子目录?并相应地在Project2common/someplace 的目标上添加一个依赖项
  • 是的,我添加了 somePlace/someOtherPlace/ 作为子目录。但是如何在项目 2 中添加依赖项? @AlexeyLarionov
  • CMake 文件的目录结构对生成的构建系统的影响相对较小。你能说明哪些目标是在哪里定义的吗?目标由add_executableadd_library 定义(或者add_custom_target,如果你正在做一些奇特的事情......)
  • @Botje 正如 AlexeyLarinov 的评论,我不能在我的 Project2 中添加一个依赖项吗?我相信它会为我做这件事,但是,我找不到适合我情况的合适的例子。我有几个嵌套的子目录,因为这个例子没有为我做:stackoverflow.com/questions/4905089/…
  • 您可以添加一个依赖项,但您需要一个由 add_executable/add_library 定义的目标,正如 Botje 建议的那样

标签: c++ cmake


【解决方案1】:

我偷看了documentation of FindProtobuf,他们在这里放了一个警告

注意:protobuf_generate_cppprotobuf_generate_python 函数和 add_executable()add_library() 调用只能在同一目录中正常工作。

因此,如果您可以将somePlace/someOtherPlace/CmakeList.txt 的内容包含在您的Project2/CmakeListst.txt 中,那么您可以安全地使用生成的文件。

我无法真正测试下面的代码,但也许在Project2/CmakeListst.txt之前使用生成的文件你可以这样做

include("${CMAKE_CURRENT_SOURCE_DIR}/../somePlace/someOtherPlace/CmakeLists.txt")

【讨论】:

  • 是的,先生。由于文档中的注释,我在 somePlace/someOtherPlace/CmakeList.txt 中使用 cmake。我稍微更新了这个问题,cmake 文件使用 add_library 如你所说。我错过了。我会尝试你的建议并更新你
  • 问题是我如何make somePlace/someOtherPlace/CmakeList.txt 与主CmakeList
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2020-11-24
相关资源
最近更新 更多