简体   繁体   English

让CMake明确选择MSVC平台工具集

[英]Have CMake select MSVC platform toolset explicitly

Hi, I have a problem, that I have a C++ project that uses select C++11/14 features that for the MSVC compiler are at best only present in the CTP_Nov2013 compiler toolset. 嗨,我有一个问题,我有一个C ++项目,该项目使用select C ++ 11/14功能,这些功能最多只能在CTP_Nov2013编译器工具集中提供,而对于MSVC编译器而言。 I am aware that there is a very similar topic here , and that the most relevant answer was that the toolset must not be tinkered with from the script, as the end-user will want to tinker with it. 我知道有一个非常类似的话题在这里 ,和最相关的答复是,该工具包不能从脚本修修补补,为最终用户会想用它修补。

  1. First of all, I do not agree that the selection of the toolset is the privilage of the end-user (whomever that might refer to in case a library under development). 首先,我不同意工具集的选择是最终用户的特权(在开发中的库中,这可能是指最终用户的特权)。
  2. Secondly, the only thing I'd like to accomplish is that CMake detects if various compiler capabilities are present, and if they are missing, AND the platform is MSVC, then depending on the version of MSVC CMake attempts to change the platform toolset to the CTP version. 其次,我要完成的唯一事情是CMake会检测是否存在各种编译器功能,如果缺少这些功能,并且平台是MSVC,则取决于MSVC的版本,CMake会尝试将平台工具集更改为CTP版本。 I know for a fact, that VS 12 will not recieve any more compiler updates (at least not in CTPs), as this is a privilage of the VS 13 Beta from here on, so it is safe to assume that MSVC_VER 1700 (if that is the version corresponding to VS 12) toolsets can be selected using the script. 我知道一个事实,VS 12将不再接受任何编译器更新(至少在CTP中不这样),因为从现在开始这是VS 13 Beta的特权,因此可以安全地假设MSVC_VER 1700(如果可以使用该脚本选择与VS 12)工具集相对应的版本。
  3. Thirdly, manually updating the generated Visual Studio project files often result in defunct project files. 第三,手动更新生成的Visual Studio项目文件通常会导致项目文件失效。 Not considering that it is tedious to change the toolset of roughly a dozen targets one by one, if the source path contains unicode characters (which they do in my case), then cl.exe will fail to find them. 如果源路径包含unicode字符(在我的情况下是这样做的),则cl.exe将无法找到它们,因此不必一一更改大约十二个目标的工具集是很麻烦的。 This is an issue of CMake, and is related to character encoding of the project file and the way CMake writes paths. 这是CMake的问题,与项目文件的字符编码以及CMake写入路径的方式有关。 There is nothing I can do about this. 我对此无能为力。 The project files MUST be ready enough for me to only have to hit F7. 项目文件必须已经准备就绪,我只需要按F7键即可。

I have found a way to achieve what I wanted with the commands 我找到了一种方法来实现我想要的命令

set(CMAKE_GENERATOR_TOOLSET "CTP_Nov2013" CACHE STRING "Platform Toolset" FORCE) 
set(CMAKE_VS_PLATFORM_TOOLSET "CTP_Nov2013" CACHE STRING "Platform Toolset" FORCE)

However the problem with this is that the script needs to be run twice in order for this to take effect. 但是,这样做的问题是脚本需要运行两次才能生效。 Since the cmake command-line is invoked using 由于cmake命令行是使用以下命令调用的

cmake -G"Visual Studio 12 2013 Win64"

There is already a toolset selected, but I immediately wish to override the toolset incorporated into the x64 Configuration of the selected generator. 已经选择了一个工具集,但是我立即希望覆盖合并到所选生成器的x64配置中的工具集。 Ultimately the user selects 32/64-bit builds from the command line as usual using the generator specification, but I wish to hide the incapabilities of the MSVC compiler from the "end-user" in case he/she has the CTP installed. 最终,用户可以像通常一样使用生成器规范从命令行中选择32/64位版本,但是如果他/她安装了CTP,我希望对“最终用户”隐藏MSVC编译器的功能。

How can I write the script and invoke it from the command line so it works for both the Visual Studio and NMake Makefiles generators and for the first invocation of CMake? 我如何编写脚本并从命令行调用它,使其对Visual Studio和NMake Makefiles生成器以及CMake的首次调用均起作用?

Using the reply of @Tanuki and set_target_properties , you can force the usage of the platform toolset in your target 使用@Tanuki和set_target_properties的回复,您可以在目标中强制使用平台工具集

if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "CTP_Nov2013")
  set_target_properties(${your_target}
  PROPERTIES
  PLATFORM_TOOLSET "${CMAKE_VS_PLATFORM_TOOLSET}")
endif()

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

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