【问题标题】:CMake: how to specify the version of Visual C++ to work with?CMake:如何指定要使用的 Visual C++ 版本?
【发布时间】:2016-02-28 07:10:23
【问题描述】:

我安装了多个版本的 Visual Studio(2010、2012、2015 试用版)。

如何强制 CMake 为特定的 VS 版本生成 makefile?默认为 VS2015 生成。

【问题讨论】:

    标签: visual-studio visual-c++ cmake version


    【解决方案1】:

    首先,您可以检查 generators 您的 CMake 版本支持的内容(以及它们的命名方式):

    > cmake.exe --help
    ...
    The following generators are available on this platform:
    ...
      Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
                                     Optional [arch] can be "Win64" or "ARM".    
    ...
    

    那么你就可以给生成器了

    1. cmake.exe -G "Visual Studio 11" ..(简称)
    2. cmake.exe -G "Visual Studio 11 2012" ..(全名)

    我更喜欢后者,因为它清晰。我通常在构建脚本包装器中有这个调用:

    @ECHO off
    IF NOT EXIST "BuildDir\*.sln" (
        cmake -H"." -B"BuildDir" -G"Visual Studio 11 2012"
    )
    cmake --build "BuildDir" --target "ALL_BUILD" --config "Release"
    

    全名被传输到一个内部缓存的 CMake 变量名CMAKE_GENERATOR。所以上面的调用等价于

    1. cmake -DCMAKE_GENERATOR="Visual Studio 11 2012" ..

    这给了我们一个有趣的可能性。如果您将一个名为 PreLoad.cmake 的文件与您的主要 CMakeLists.txt 文件平行放置,您可以强制将默认值(如果可用)用于您的项目

    1. cmake.exe ..

      PreLoad.cmake

      if (NOT "$ENV{VS110COMNTOOLS}" STREQUAL "")
          set(CMAKE_GENERATOR "Visual Studio 11 2012" CACHE INTERNAL "Name of generator.")
      endif()
      

    有时您可能还需要添加-T <toolset-name>-A <platform-name> 选项:

    1. cmake.exe -G "Visual Studio 10" -T "v90" ..

    最后但并非最不重要的是,如果您真的只对编译器感兴趣

    1. "\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"

      cmake.exe -G "NMake Makefiles" ..


    参考文献

    【讨论】:

    • 一个重要问题的重要答案。为你 +1。
    • 有没有办法将生成器设置为 MSVC 并使其使用 CLANG 作为编译器?似乎无论我做什么-G"Visual Studio 15 2017 Win64" 我都不能让 CMake 使用不同的编译器(尽管它是兼容的)。
    • 这对我不起作用,我指出-G"Visual Studio 15 2017 Win64",但仍然显示-- The C compiler identification is MSVC 19.16.27045.0 -- The CXX compiler identification is MSVC 19.16.27045.0
    【解决方案2】:
    cmake -G "Visual Studio 12" ..\MyProject
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多