Run CMake from within a Visual Studio command prompt window: Windows Start Menu -> Programs -> Visual Studio 2015 -> Visual Studios Tools -> Windows Desktop Comand Prompts -> whatever is appropriate. These are just Windows BAT files that set up the environment paths and C++ compiler options to allow them to be called from the command line or programs like CMake.

I have several BAT files the I link to from my Desktop that execute the appropriate VS BAT file and then CD to the directory where my CMake project lives. For example, this "BuildProject.BAT" file lives at the top level of my CMake project and is called with these command line arguments:

Qt Code:
  1. C:\> BuildProject [clean] [Win32 | Win64] [Release | Debug]
To copy to clipboard, switch view to plain text mode 

where all arguments are optional. The project builds to an out-of-source directories ("build" and "install") that have subdirectories according to the bitness and build type.

If you call it with no arguments, it will rebuild anything that has changed since the last build, except the CMake Makefiles.

If you call it with the argument "clean", it will erase everything in the output directories and rebuild everything from scratch, including regenerating the Makefiles from the CMakeLists.txt inputs. It will build Win32 Release, Win32 Debug, Win64 Release, and Win64 Debug.

If you call it this way:

Qt Code:
  1. BuildProject foo Win32 Release
To copy to clipboard, switch view to plain text mode 

it won't clean (anything other than "clean" as the first argument works) and will build only the 32-bit Release version. You can put "foo" to substitute for any of the three arguments and it will perform the default:

arg 1 = foo: no clean, just build
arg 2 = foo: builds both 32- and 64 bit versions
arg 3 = foo: builds both Release and Debug versions

so you can have things like:
Qt Code:
  1. BuildProject clean foo Release <-- cleans, then builds 32- and 64-bit Release versions
  2. BuildProject foo Win32 foo <-- builds 32-bit Release and Debug versions after source changes
  3. BuildProject clean Win64 Release <-- cleans, then builds only 64-bit Release version
  4. BuildProject foo foo foo <-- identical to BuildProject (with no arguments)
To copy to clipboard, switch view to plain text mode 

Here's the BuildProject.bat file. Note that it also has a variable (vsversion) that lets me customize it for the Visual Studio version I want to use:
Qt Code:
  1. @echo off
  2.  
  3. REM Clean, generate, and build Windows 32- and 64-bit debug and release
  4. REM versions of the project DLLs and executables
  5.  
  6. REM Change for a different Visual Studio version. 12.0 = MSVC 2013, 14.0 = MSVC 2015, etc.
  7. set vsversion=12.0
  8.  
  9. if "%1" == "clean" (
  10. echo -- Cleaning
  11. if EXIST install\libs\Windows rd /s /q install\libs\Windows
  12. if EXIST install\Include rd /s /q install\Include
  13. if EXIST build\Win32 rd /s /q build\Win32
  14. if EXIST build\Win64 rd /s /q build\Win64
  15. echo -- Cleaning done
  16. echo --
  17. )
  18.  
  19. set bitness=Win32 Win64
  20. if "%2" == "Win32" (
  21. set bitness=Win32
  22. )
  23.  
  24. if "%2" == "Win64" (
  25. set bitness=Win64
  26. )
  27.  
  28. set build=Release Debug
  29. if "%3" == "Release" (
  30. set build=Release
  31. )
  32.  
  33. if "%3" == "Debug" (
  34. set build=Debug
  35. )
  36.  
  37. REM This is required in order for "machine" and "arch" to be evaluated within the loops
  38. setlocal EnableDelayedExpansion
  39. for %%M in ( %build% ) do (
  40. for %%W in ( %bitness% ) do (
  41. REM and this ensures that any environment changes made by vcvarsall are scoped
  42. REM by the inner loop
  43.  
  44. setlocal
  45. if %%W == Win32 (
  46. set machine=x86
  47. set arch=x86
  48. ) else (
  49. set machine=amd64
  50. set arch=x64
  51. )
  52.  
  53. echo -- Starting %%W %%M
  54. if NOT EXIST build\%%W\%%M mkdir build\%%W\%%M
  55. cd build\%%W\%%M
  56. call "%ProgramFiles(x86)%\Microsoft Visual Studio %vsversion%\VC\vcvarsall" !machine!
  57. echo -- Generating %%W %%M
  58. cmake -G"NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake -DBUILD_TYPE=%%M -DTARGET_ARCH=!arch! ..\..\..
  59. echo -- Building %%W %%M
  60. cmake --build . 2>&1 >build.log
  61. echo -- Installing %%W %%M
  62. cmake -P cmake_install.cmake
  63. echo -- Finished %%W %%M
  64. cd ..\..\..
  65. endlocal
  66. )
  67. )
To copy to clipboard, switch view to plain text mode 

The windows_toolchain.cmake sets up some CMake options for building my particular project:

Qt Code:
  1. # target operating system
  2. set (CMAKE_SYSTEM_NAME Windows)
  3. set (WINDOWS true)
  4.  
  5. set (CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "Choose the type of build" FORCE)
  6. set (CMAKE_CXX_FLAGS "/D WIN32 /EHsc /D UNICODE /D _UNICODE /D _WINDLL /D XERCES_STATIC_LIBRARY")
  7. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags" )
To copy to clipboard, switch view to plain text mode