<small id='ZcSvF'></small><noframes id='ZcSvF'>

  • <legend id='ZcSvF'><style id='ZcSvF'><dir id='ZcSvF'><q id='ZcSvF'></q></dir></style></legend>
      <bdo id='ZcSvF'></bdo><ul id='ZcSvF'></ul>

      <i id='ZcSvF'><tr id='ZcSvF'><dt id='ZcSvF'><q id='ZcSvF'><span id='ZcSvF'><b id='ZcSvF'><form id='ZcSvF'><ins id='ZcSvF'></ins><ul id='ZcSvF'></ul><sub id='ZcSvF'></sub></form><legend id='ZcSvF'></legend><bdo id='ZcSvF'><pre id='ZcSvF'><center id='ZcSvF'></center></pre></bdo></b><th id='ZcSvF'></th></span></q></dt></tr></i><div id='ZcSvF'><tfoot id='ZcSvF'></tfoot><dl id='ZcSvF'><fieldset id='ZcSvF'></fieldset></dl></div>

      <tfoot id='ZcSvF'></tfoot>

      1. 如何使用 Git 子模块和 CMake 处理传递依赖冲突?

        时间:2023-08-28
            <tbody id='xunI9'></tbody>
            <legend id='xunI9'><style id='xunI9'><dir id='xunI9'><q id='xunI9'></q></dir></style></legend>

              1. <small id='xunI9'></small><noframes id='xunI9'>

                • <bdo id='xunI9'></bdo><ul id='xunI9'></ul>
                  <tfoot id='xunI9'></tfoot>

                  <i id='xunI9'><tr id='xunI9'><dt id='xunI9'><q id='xunI9'><span id='xunI9'><b id='xunI9'><form id='xunI9'><ins id='xunI9'></ins><ul id='xunI9'></ul><sub id='xunI9'></sub></form><legend id='xunI9'></legend><bdo id='xunI9'><pre id='xunI9'><center id='xunI9'></center></pre></bdo></b><th id='xunI9'></th></span></q></dt></tr></i><div id='xunI9'><tfoot id='xunI9'></tfoot><dl id='xunI9'><fieldset id='xunI9'></fieldset></dl></div>
                • 本文介绍了如何使用 Git 子模块和 CMake 处理传递依赖冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们有许多 Git 存储库,一些包含我们自己的代码,一些包含稍微修改的第三方库代码.简化的依赖关系图如下所示:

                  We have a number of Git repositories, some containing our own code and some containing slightly modified third-party library code. A simplified dependency graph looks like this:

                    executable_A
                      |     |
                      |     v
                      |  library_B
                      |     |
                      v     v
                     library_C
                  

                  所以可执行文件对 library_C 有两个依赖,一个是直接的,一个是传递的.我希望使用 Git 子模块和 CMake 将这一切联系在一起,因此简化的目录结构如下所示:

                  So the executable has two dependencies on library_C, one direct and one transitive. I am hoping to tie this all together using Git submodules and CMake, so a simplified directory structure looks like this:

                  executable_A/
                    CMakeListst.txt
                    library_B/
                      CMakeLists.txt
                      library_C/
                        CMakeLists.txt
                    library_C/
                      CMakeLists.txt
                  

                  如您所见,library_C 存储库作为子模块被包含两次.让我们假设两个子模块都指向同一个提交(任何关于如何强制执行的想法都是受欢迎的,但不是这个问题的主题).

                  As you can see, the library_C repository is included as a submodule twice. Let's assume that both submodules are pointing at the same commit (any ideas about how to enforce that would be welcome, but are not the topic of this question).

                  我们使用 add_subdirectorytarget_link_librariestarget_include_directories 来管理这些相互依赖关系.很标准.

                  We're using add_subdirectory, target_link_libraries and target_include_directories to manage these interdependencies. Pretty standard.

                  问题是CMake不喜欢你创建一个同名的目标两次,所以它会抱怨:

                  The problem is that CMake doesn't like it if you create a target with the same name twice, so it complains:

                  library_C/CMakeLists.txt 中的 CMake 错误:13 (add_library):
                  add_library 无法创建目标library_C",因为另一个目标同名已经存在.现有目标是静态的在源目录.../library_B/library_C"中创建的库.
                  如需了解详情,请参阅政策 CMP0002 的文档.

                  CMake Error at library_C/CMakeLists.txt:13 (add_library):
                  add_library cannot create target "library_C" because another target with the same name already exists. The existing target is a static library created in source directory ".../library_B/library_C".
                  See documentation for policy CMP0002 for more details.

                  我宁愿不删除 executable_Alibrary_C 的直接依赖,因为它是通过 library_B 拉进来的不应依赖的 library_B 的实现细节.此外,一旦我们添加另一个依赖项(如 executable_A -->),这种方法就会失效.library_D -->library_C.

                  I'd rather not remove the direct dependency of executable_A on library_C, because the fact that it is pulled in via library_B is an implementation detail of library_B that should not be relied on. Moreover, this approach will break down as soon as we add another dependency like executable_A --> library_D --> library_C.

                  (这个问题 是我能找到的最接近的,但更笼统,无论如何仍然没有答案.)

                  (This question is the closest I could find, but is a bit more general and remains unanswered anyway.)

                  推荐答案

                  有几种方法可以检测和丢弃项目的包含,这些项目已经包含在主项目的其他部分.

                  There are several approaches for detect and discard inclusion of the project, which has already be included in some other parts of the main project.

                  单个包含子项目的最简单模式是检查某个子项目的目标是否存在:

                  The simplest pattern for single inclusion of subproject is checking existence of some subproject's target:

                  # When include 'C' subproject
                  if(NOT TARGET library_C)
                      add_subdirectory(C)
                  endif()
                  

                  (这里我们假设项目 C 定义了目标 library_C.)

                  (Here we assume that project C defines target library_C.)

                  在这种有条件的包含之后,所有子项目的目标和功能将立即提供给调用者保证.

                  After such conditional inclusion all subproject's targets and functions will be immediately available for the caller with garantee.

                  最好在所有地方(在executable_Alibrary_B 中)使用此模式.这种改变executable_Alibrary_Blibrary_C的顺序的方式不会破坏正确性.

                  It is better to use this pattern in all places (in executable_A and library_B). Such a way changing order of library_B and library_C in executable_A doesn't break correctness.

                  这个模式可以重新设计以供子项目本身使用:

                  This pattern can be reworked for use by subproject itself:

                  # At the beginning of 'C' project
                  cmake_minimum_required(...)
                  if(TARGET library_C)
                      return() # The project has already been built.
                  endif()
                  
                  project(C)
                  ...
                  

                  <小时>

                  检查项目是否存在

                  创建项目时,CMake 为其定义了几个变量,_BINARY_DIR 就是其中之一.注意,这个变量是缓存,所以当 cmake 被第二次调用时(例如,如果 CMakeLists.txt 的某些部分已经改变),变量在一开始就存在.


                  Check project existence

                  When a project is created, CMake defines several variables for it, and <PROJECT-NAME>_BINARY_DIR is among them. Note, that this variable is cached, so when cmake is called the second time (e.g. if some of CMakeLists.txt has been changed), the variable exists at the very beginning.

                  # When include 'C' subproject
                  if(NOT C_BINARY_DIR # Check that the subproject has never been included
                      OR C_BINARY_DIR STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/C" # Or has been included by us.
                  )
                      add_subdirectory(C)
                  endif()
                  

                  这个模式可以重新设计以供子项目本身使用:

                  This pattern can be reworked for use by subproject itself:

                  # At the beginning of 'C' project
                  cmake_minimum_required(...)
                  if(NOT C_BINARY_DIR # Check that the project has never been created
                      OR C_BINARY_DIR STREQUAL "${CMAKE_CURRENT_BINARY_DIR}" # Or has been created by us.
                      project(C)
                  else()
                      return() # The project has already been built
                  endif()
                  

                  这篇关于如何使用 Git 子模块和 CMake 处理传递依赖冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CMake:无法运行 MSBuild 命令:MSBuild.exe 下一篇:如何在 linux 上通过 cmake 链接 google protobuf 库?

                  相关文章

                  • <bdo id='38FIW'></bdo><ul id='38FIW'></ul>
                  <i id='38FIW'><tr id='38FIW'><dt id='38FIW'><q id='38FIW'><span id='38FIW'><b id='38FIW'><form id='38FIW'><ins id='38FIW'></ins><ul id='38FIW'></ul><sub id='38FIW'></sub></form><legend id='38FIW'></legend><bdo id='38FIW'><pre id='38FIW'><center id='38FIW'></center></pre></bdo></b><th id='38FIW'></th></span></q></dt></tr></i><div id='38FIW'><tfoot id='38FIW'></tfoot><dl id='38FIW'><fieldset id='38FIW'></fieldset></dl></div>

                  <legend id='38FIW'><style id='38FIW'><dir id='38FIW'><q id='38FIW'></q></dir></style></legend>
                • <tfoot id='38FIW'></tfoot>

                      <small id='38FIW'></small><noframes id='38FIW'>