Visual Studio的Cmake生成器不设置CMAKE_CONFIGURATION_TYPES [英] Cmake generators for Visual Studio do not set CMAKE_CONFIGURATION_TYPES

查看:4303
本文介绍了Visual Studio的Cmake生成器不设置CMAKE_CONFIGURATION_TYPES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cmake常见问题

其他
地方
建议检查 CMAKE_CONFIGURATION_TYPES 来识别多配置生成器。我发现了几个问题,这不工作(例如这一个)。似乎是第一次调用cmake时未设置该变量。

The Cmake FAQ and other places recommend to check CMAKE_CONFIGURATION_TYPES to recognize a multi-configuration generator. I have found several questions where this did not work (for example this one). The issue seems to be that the variable is not set the first time cmake is called.

我使用以下文件测试了

cmake_minimum_required(VERSION 2.6)

if(CMAKE_CONFIGURATION_TYPES)
    message("Multi-configuration generator")
else()
    message("Single-configuration generator")
endif()

project(foo)

并像这样调用

mkdir build
cd build
cmake -G "Visual Studio 12 2013" ..

并获得单配置生成器

如何区分当前生成器是否支持多种配置?

How should I distinguish whether the current generator supports multiple configurations?

推荐答案

已修改:添加了有关检查并更改的信息 CMAKE_CONFIGURATION_TYPES

EDITED: Added information on checking and changing CMAKE_CONFIGURATION_TYPES

此问题,您可以检查并更改 CMAKE_CONFIGURATION_TYPES ,但可以意识:存在错误 0015577:项目命令覆盖CMAKE_CONFIGURATION_TYPES在CMake 3.2.2中,它破坏了初始VS解决方案生成的行为(用CMake 3.3.0修复):

Taking the suggestions from this question you could check and change CMAKE_CONFIGURATION_TYPES, but be aware that there was a bug 0015577: The 'project' command overwrites CMAKE_CONFIGURATION_TYPES in CMake 3.2.2 that did break this behaviour for the initial VS solution generation (fixed with CMake 3.3.0):

cmake_minimum_required(VERSION 3.3)

project(foo NONE)

if(CMAKE_CONFIGURATION_TYPES)
    message("Multi-configuration generator")
    set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "My multi config types" FORCE)
else()
    message("Single-configuration generator")
endif()

enable_language(C CXX)



预设 CMAKE_CONFIGURATION_TYPES



如果你只需要一套配置的多配置环境,你可以做(​​感谢@Tsyvarev的建议):

Preset CMAKE_CONFIGURATION_TYPES

If you just need a certain set of configurations for multi-configuration environments you can do (thanks to @Tsyvarev for the suggestion):

cmake_minimum_required(VERSION 2.8)

# NOTE: Only used in multi-configuration environments
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "My multi config types" FORCE)

project(foo)


$ b b

无多配置环境将忽略它。但请注意,其他CMake模块如 findBoost.cmake findCUDA.cmake CMAKE_CONFIGURATION_TYPES 对于单配置环境为空(再次感谢@Tsyvarev提示)。

None multi-configuration environments will just ignore it. But be aware that other CMake modules like findBoost.cmake, findCUDA.cmake may rely on CMAKE_CONFIGURATION_TYPES being empty for single-configuration environments (thanks again @Tsyvarev for the hint).

解决方案将添加所有支持的生成器的工具链文件。它们通常很有用,因为你可以处理所有的工具链/发电机特定的部分。

So a better solution would be adding toolchain files for all your supported generators. They are generally useful, because there you can handle all the toolchain/generator specific parts.

以下是我的 VSToolchain.txt 的摘录:

# Reduce the config types to only Debug and Release
SET(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)

# Standard is a console app. If you need a windows app, use WIN32 define in add_executable
set(CMAKE_WIN32_EXECUTABLE 0 CACHE INTERNAL "")


$ b b

CMAKE_WIN32_EXECUTABLE 就是为了显示我在Visual Studio工具链文件中设置了什么样的设置。

CMAKE_WIN32_EXECUTABLE is just there to show what kind of settings I have put in my Visual Studio toolchain file.

另一个CMake命令行解决方案建议在这里:

Another CMake command line solution is suggested here: How to create cmake build configuration without debug symbols and without optimizations?

如果您只想查看CMake在 CMAKE_CONFIGURATION_TYPES 中设置的内容:

If you only want do check what CMake does set in CMAKE_CONFIGURATION_TYPES:

我刚刚使用Visual Studio 2013和MinGW / GCC(两个都有空的 build 目录)测试了上面的代码。您只需要一个小的更改,并移动 project()命令后的支票:

I just tested your above code with Visual Studio 2013 and MinGW/GCC (both with empty build directories). You just need one small change and move the check after the project() command:

project(foo)

message("CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES}")

if(CMAKE_CONFIGURATION_TYPES)
    message("Multi-configuration generator")
else()
    message("Single-configuration generator")
endif()

我得到VS2013:

CMAKE_CONFIGURATION_TYPES Debug;Release;MinSizeRel;RelWithDebInfo
Multi-configuration generator

对于GCC:

CMAKE_CONFIGURATION_TYPES
Single-configuration generator

有关CMake看到的详细信息:

For more details about what CMake does see:

  • CMAKE_CONFIGURATION_TYPES set by EnableLanguage() in cmGlobalVisualStudio7Generator.cxx
  • CMake: In which Order are Files parsed (Cache, Toolchain, …)?

这篇关于Visual Studio的Cmake生成器不设置CMAKE_CONFIGURATION_TYPES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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