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

查看:30
本文介绍了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.

我使用以下文件进行了测试

I tested with the following file

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?

推荐答案

EDITED:添加了关于检查和更改的信息CMAKE_CONFIGURATION_TYPES

EDITED: Added information on checking and changing CMAKE_CONFIGURATION_TYPES

根据这个问题的建议,您可以检查和更改CMAKE_CONFIGURATION_TYPES,但意识到存在一个错误 0015577:项目"命令覆盖CMake 3.2.2 中的 CMAKE_CONFIGURATION_TYPES 确实打破了初始 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)

没有多配置环境会忽略它.但是请注意其他 CMake 模块,如 findBoost.cmakefindCUDA.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 "")

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 命令行解决方案:如何在没有调试符号和优化的情况下创建 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:

And I get for VS2013:

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

对于海湾合作委员会:

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天全站免登陆