CMake: how to specify the version of Visual C++ to work with?

我与影子孤独终老i 提交于 2019-11-26 21:11:25

问题


I have multiple versions of Visual Studio installed (2010, 2012, 2015 trial).

How can I force CMake to generate the makefiles for a specific VS version? By default it generates for VS2015.


回答1:


First you can check what generators your CMake version does support (and how they are named):

> 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".    
...

Then you can give the generator with

  1. cmake.exe -G "Visual Studio 11" .. (short name)
  2. cmake.exe -G "Visual Studio 11 2012" .. (full name)

I prefer the later, because of its clarity. And I normally have this call in a build script wrapper:

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

The full name is transferred to an internal cached CMake variable name CMAKE_GENERATOR. So the above calls are equivalent to

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

This gives us an interesting possibility. If you place a file called PreLoad.cmake parallel to your main CMakeLists.txt file you can force the default (if available) to take for your project there

  1. cmake.exe ..

    PreLoad.cmake

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

Sometimes you may need to add also -T <toolset-name> or -A <platform-name> option:

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

And last but not least if you are really only interested in the compiler

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

    cmake.exe -G "NMake Makefiles" ..


References

  • CMake command line
  • What is the default generator for CMake in Windows?
  • CMake: In which Order are Files parsed (Cache, Toolchain, …)?
  • How can I generate a Visual Studio 2012 project targeting Windows XP with CMake?



回答2:


cmake -G "Visual Studio 12" ..\MyProject


来源:https://stackoverflow.com/questions/33917454/cmake-how-to-specify-the-version-of-visual-c-to-work-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!