简体   繁体   English

使CMake Visual Studio中的所有项目都依赖一个项目

[英]Making all projects in CMake Visual Studio depend on one project

In my project, I have about 250 projects with one main project that uses most of the projects. 在我的项目中,我大约有250个项目,其中一个主要项目使用了大多数项目。 It's important that all projects are up to date when the main project is run. 在运行主项目时,所有项目都是最新的,这一点很重要。 So basically, Visual Studio should check for all 250 projects for changes when MainProject is compiled (and run). 因此,基本上,Visual Studio应该在MainProject编译(并运行)时检查所有250个项目的更改。 My CMakeLists.txt files look like this. 我的CMakeLists.txt文件如下所示。

Root/CMakeLists.txt 根目录/CMakeLists.txt

....
add_subdirectory (MainProject)
add_subdirectory (ProjectA)
add_subdirectory (ProjectB)
add_subdirectory (ProjectC)
add_subdirectory (ProjectD)
....

Root/MainProject/CMakeLists.txt 根目录/MainProject/CMakeLists.txt

....
add_executable (MainProject a.cpp b.cpp)
add_dependencies (MainProject ProjectA ProjectB ...)
....

Root/ProjectA/CMakeLists.txt 根目录/ProjectA/CMakeLists.txt

....
add_executable (ProjectA a.cpp b.cpp)
....

Obviously this is a very simplified example, but hopefully the idea is there. 显然,这是一个非常简化的示例,但希望这个想法在那里。 Basically, in order to make Visual Studio to check for dependencies for all 250 projects or so, I have to add all the other projects in the main project as dependencies. 基本上,为了使Visual Studio能够检查所有250个左右项目的依赖关系,我必须将主项目中的所有其他项目添加为依赖项。 Now this is not an elegant solution at all, as add_dependencies in MainProject has a LOT of dependencies in it. 现在这根本不是一个优雅的解决方案,因为MainProject中的add_dependencies中有很多依赖项。 It works, but is there anything more elegant for this problem? 它可以工作,但是对于这个问题还有什么更好的选择吗?

Turning my comments into an answer 把我的评论变成答案

At the moment (as per CMake version 3.5.x), there is - as far as I know - no global target list provided by CMake itself (eg as a global property). 目前(根据​​CMake版本3.5.x),据我所知,没有CMake本身提供的全局目标列表(例如,作为全局属性)。

Edit: It is now implemented: Global BUILDSYSTEM_TARGETS property was released with CMake 3.7 编辑:现在实现:CMake 3.7发布了全局BUILDSYSTEM_TARGETS属性

Posible Solutions 可能的解决方案

  1. In your case - going by the assumption that you don't want to modify all 250 sub-project's CMakeLists.txt files - overwriting add_executable() , add_library() and add_custom_target() would do the trick: 在您的情况下-假设您不想修改所有250个子项目的CMakeLists.txt文件-覆盖add_executable()add_library()add_custom_target()可以达到目的:

     cmake_minimum_required(VERSION 2.8) project(DependsAllTest) macro(add_library _target) _add_library(${_target} ${ARGN}) set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target}) endmacro() macro(add_executable _target) _add_executable(${_target} ${ARGN}) set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target}) endmacro() macro(add_custom_target _target) _add_custom_target(${_target} ${ARGN}) set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target}) endmacro() add_subdirectory(MainProject) add_subdirectory(ProjectA) add_subdirectory(ProjectB) add_subdirectory(ProjectC) add_subdirectory(ProjectD) get_property(_allTargets GLOBAL PROPERTY GlobalTargetList) message(STATUS "GlobalTargetList: ${_allTargets}") add_dependencies(MainProject ${_allTargets}) 

    Sure enough, if you would do this from scratch I would - as @Lindydancer has suggested - use your own versions of those commands to allow global customizations. 果然,如果您从头开始执行此操作,我将按照@Lindydancer的建议,使用您自己的那些命令版本来允许全局自定义。

  2. If you go by the prerequisite that MainProject is always first, you could simplify this a little: 如果MainProject始终是第一位的前提,则可以简化一下:

     cmake_minimum_required(VERSION 2.8) project(DependsAllTest2) macro(add_library _target) _add_library(${_target} ${ARGN}) add_dependencies(MainProject ${_target}) endmacro() macro(add_executable _target) _add_executable(${_target} ${ARGN}) add_dependencies(MainProject ${_target}) endmacro() macro(add_custom_target _target) _add_custom_target(${_target} ${ARGN}) add_dependencies(MainProject ${_target}) endmacro() add_subdirectory(MainProject) add_subdirectory(ProjectA) add_subdirectory(ProjectB) add_subdirectory(ProjectC) add_subdirectory(ProjectD) 

References 参考文献

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM