在带有子项目的项目中的哪里设置CMAKE_CONFIGURATION_TYPES [英] Where to set CMAKE_CONFIGURATION_TYPES in a project with subprojects

查看:154
本文介绍了在带有子项目的项目中的哪里设置CMAKE_CONFIGURATION_TYPES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个包含两个独立子项目的项目。如果我正确理解了cmake,则想法是使用一个根 CMakeLists.txt 定义一个 project(...),然后使用 add_subdirectory(...)包含子项目。每个子项目都有自己的 CMakeLists.txt 来定义自己的项目。这样,项目既可以一起构建(使用根cmake文件),也可以单独构建(使用子项目cmake文件)。

Lets say I have a project with two independent subprojects. If I understood cmake correctly, the idea would be to have one root CMakeLists.txt defining a project(...) and then using add_subdirectory(...) to include the subprojects. Each subproject would have its own CMakeLists.txt defining its own project. This way projects can be build either together (using the root cmake file) or individually (using the subprojects cmake file).

我现在想更改 CMAKE_CONFIGURATION_TYPES 。我应该在根 CMakeLists.txt 中还是在每个子项目中,或者同时在这两个子项目中这样做?

I now would like to change the CMAKE_CONFIGURATION_TYPES. Should I do this in the root CMakeLists.txt or in each subproject, or both?

在root意味着单独构建一个子项目将提供错误的配置类型;其他选项将复制cmake代码。我想我在这里丢失了一些东西。

Changing it in the root would mean that building a subproject individually would offer the wrong configuration types; the other options would duplicate the cmake code. I think I'm missing something here.

推荐答案

分解出设置与配置有关的设置的代码。创建具有以下内容的文件,例如 SetUpConfigurations.cmake

Factorize out the code that sets up configuration-dependent settings. Create a file, say, SetUpConfigurations.cmake with this content:

if(NOT SET_UP_CONFIGURATIONS_DONE)
    set(SET_UP_CONFIGURATIONS_DONE TRUE)

    # No reason to set CMAKE_CONFIGURATION_TYPES if it's not a multiconfig generator
    # Also no reason mess with CMAKE_BUILD_TYPE if it's a multiconfig generator.
    get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
    if(isMultiConfig)
        set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Profile" CACHE STRING "" FORCE) 
    else()
        if(NOT CMAKE_BUILD_TYPE)
            message("Defaulting to release build.")
            set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
        endif()
        set_property(CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING "Choose the type of build")
        # set the valid options for cmake-gui drop-down list
        set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;Profile")
    endif()
    # now set up the Profile configuration
    set(CMAKE_C_FLAGS_PROFILE "...")
    set(CMAKE_CXX_FLAGS_PROFILE "...")
    set(CMAKE_EXE_LINKER_FLAGS_PROFILE "...")
endif()

然后 include(..)此文件位于 CMakeLists.txt 的开头。

Then include(..) this file at the beginning of the CMakeLists.txt's.

关于放置 SetUpConfigurations.cmake 的位置,您有两种选择,这取决于您如何组织项目,存储库:

You have two choices about where to put SetUpConfigurations.cmake, it depends on how you organize your projects, repositories:


  1. 快捷方式:将脚本复制并提交到需要它的每个项目中。相对于项目的 CMakeLists.txt ,其位置将是固定的。因此,您可以将其包含在例如 include($ {CMAKE_CURRENT_SOURCE_DIR} /< ...> /SetUpConfigurations.cmake)

  1. The quick'n'dirty way: Copy and commit this script into each project that needs it. Its location will be fixed, relative to the CMakeLists.txt of the project. So you can include it, for example, with include(${CMAKE_CURRENT_SOURCE_DIR}/<...>/SetUpConfigurations.cmake)

纪律化的方式:使用您的自定义CMake脚本维护一个存储库,就像这样。每次使用 cmake 命令生成一个项目时,都会在 CMAKE_MODULE_PATH 变量中将路径传递到该存储库: / p>

The disciplined way: Maintain a repository with your custom CMake scripts, like this one. Each time you generate a project with the cmake command, you pass the path to this repository in the CMAKE_MODULE_PATH variable:

cmake -DCMAKE_MODULE_PATH=<dir-of-cmake-script-repo> ...


在这种情况下,请包含脚本带有 include(SetUpConfigurations)(无 .cmake 扩展名)。

In this case include the script with include(SetUpConfigurations) (no .cmake extension).

关于多配置生成器的注释:

A note about what a multiconfig generator is:

Xcode Visual Studio 是multiconfig生成器。他们尊重 CMAKE_CONFIGURATION_TYPES 的值,但是 CMAKE_BUILD_TYPE 无效,因为 CMakeLists.txt 已处理。

Xcode and Visual Studio are multiconfig generators. They respect the value of CMAKE_CONFIGURATION_TYPES but CMAKE_BUILD_TYPE has no effect since no concrete configuration is defined when the CMakeLists.txt is processed. It will be selected on the IDE's user interface later.

另一方面,makefile样式的生成器对 CMAKE_CONFIGURATION_TYPES CMAKE_BUILD_TYPE 定义配置。当处理 CMakeLists.txt 文件时,这是一个具体值,但仍然:切勿根据 CMAKE_BUILD_TYPE

On the other hand, the makefile-style generators are not interested in CMAKE_CONFIGURATION_TYPES. CMAKE_BUILD_TYPE defines the configuration. It is a concrete value when the CMakeLists.txt file is processed but still: never make any decisions based on the value of CMAKE_BUILD_TYPE:

if(CMAKE_BUILD_TYPE STREQUAL "Release") # WRONG!
    ....
endif()

您的项目将无法正常工作如multiconfig生成器中的预期。

You project won't work as intended in multiconfig generators.

这篇关于在带有子项目的项目中的哪里设置CMAKE_CONFIGURATION_TYPES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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