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

I have several versions of Visual Studio (2010, 2012, 2015).

How can I get CMake to generate makefiles for a specific version of VS? By default, it generates for VS2015.

+8
visual-c ++ visual-studio cmake version
Nov 25 '15 at 13:14
source share
2 answers

You can first check that generators your version of CMake supports (and what they are called):

> 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

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

I prefer later, because of its clarity. And I usually call in the shell script:

 @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 passed to the internal cached variable name CMake CMAKE_GENERATOR . So the above calls are equivalent

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


This gives us an interesting opportunity. If you put a file called PreLoad.cmake parallel to the 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 the -T <toolset-name> or -A <platform-name> parameter as well:

  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 CMake generator on Windows?
  • CMake: in what order are the files processed (Cache, Toolchain, ...)?
  • How can I create a Visual Studio 2012 project targeted to Windows XP using CMake?
+10
Nov 25 '15 at 20:44
source share
 cmake -G "Visual Studio 12" ..\MyProject 
0
Nov 25 '15 at 13:19
source share



All Articles