c++ - 管理 cmake 依赖 git 模块

标签 c++ cmake git-submodules

我正在尝试为我的一个库设置 cmake 构建。假设我想要依赖于 A 的库 B。这两个库都使用 CMake 作为构建系统。两者都通过 git submodule 对 gtest 具有外部依赖性。

.
+-libA
  |-CMakeLists.txt
  |...
  +-external/gtest
    +-CMakeLists.txt
+-libB
  |-CMakeLists.txt
  |...
  +-external/gtest
    +-CMakeLists.txt

能够独立构建项目并作为更大项目的一部分的最佳项目结构是什么?

使用上面的布局我得到一个错误,gtest 是由另一个项目定义的:

CMake Error at libstyxe/external/gtest/googletest/cmake/internal_utils.cmake:161 (add_library):                                               
  add_library cannot create target "gtest" because another target with the                                                                    
  same name already exists.  The existing target is a static library created                                                                  
  in source directory                                                                                                                         
  "libsolace/external/gtest/googletest".  See                                                                 
  documentation for policy CMP0002 for more details. 

我尝试过的选项: 1. 让 libA 从 libB/external 建立符号链接(symbolic link),并通过 add_directory 包含它。 Builind libB 失败并出现与上述类似的错误。 2. 创建一个根 CMakeLists.txt 添加 libA 和 libB 作为 add_subdirectory。 同样的问题。

在 libA/CMakeLists.txt 和 libB/CMakeLists.txt 中,gtest 包含为: 添加子目录(外部/gtest/googletest EXCLUDE_FROM_ALL)

最佳答案

您可以通过在主 CMakeLists.txt 顶部附近添加以下行来禁用对当前项目重复名称的检查:

cmake_policy(SET CMP0002 OLD)

禁用这些检查可能会给您带来其他问题,但这可能只是您需要的临时解决方法。

关于c++ - 管理 cmake 依赖 git 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843769/

相关文章:

c++ - 异步串行通信 : why does ReadFile() set the event in the OVERLAPPED struct?

c++ - 如何在 Visual Studio 中的干净构建期间删除 dlldata.c?

c++ - CMake find_* 不搜索子目录

git - 如何将 git 子模块与 Bitbucket 管道一起使用?

ios - 从子模块引用 pod 时“没有这样的模块”

c++ - 在两个分母的 LCM 都很大的 chrono::duration 类型之间转换

c++ - 从字符串中提取字符

Qt Creator 在树莓派上没有 CMake 生成器

linux - cmake 找不到 Qt5

git - 使用 Git,子树 merge 具有子模块的外部项目的最佳方法是什么?