如何强制 cmake 使用 Visual Studio 2015 (v140) 平台工具集?

How to force cmake to use Visual Studio 2015 (v140) platform toolset?

我安装了 VS2015 和 VS2017。对于一个cmake项目,我想用cmake生成一个vs2015项目(platform toolset v140)。

我打开了 Developer Command Prompt for VS2015 然后我 运行 cmake 在那里。 但是从输出和结果 sln 文件中,我看到它仍在使用 vs2017 (v141) 平台工具集。

-- Building for: Visual Studio 15 2017 -- The C compiler identification is MSVC 19.16.27027.1 -- The CXX compiler identification is MSVC 19.16.27027.1 -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works

请在此处查看屏幕截图: cmake vs2015

那么,如何强制cmake使用vs2015?

cmake -G "Visual Studio 14 2015"

确保 Windows SDK version 10.0.10586.0 已安装。

查看生成器列表,运行 cmake --help

这就是 -T option 的用途:

-T Specify toolset name if supported by generator.

Some CMake generators support a toolset name to be given to the native build system to choose a compiler. See the CMAKE_GENERATOR_TOOLSET variable. This is supported only on specific generators:

Visual Studio Generators for VS 2010 and above The Xcode generator for Xcode 3.0 and above See native build system documentation for allowed toolset names.

cmake -G "Visual Studio 15 2017" -T v140

(请注意,Visual Studio 的生成器名称在 CMake 3.13 和 3.14 之间发生了变化)

这将使用 2015 编译器和工具链生成 Visual Studio 2017 解决方案。所以打开解决方案将打开 VS 2017,但代码将使用 2015 工具链编译。

如果您想改用 2015 IDE,请使用 2015 生成器并省略 -T 参数。

请注意,您还可以将 v140 工具链作为 VS2017 安装的一部分进行安装,因此,如果您不打算使用较旧的 IDE,则确实没有理由保留 VS2015 安装。