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:
C:\> BuildProject [clean] [Win32 | Win64] [Release | Debug]
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:
BuildProject foo Win32 Release
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:
BuildProject clean foo Release <-- cleans, then builds 32- and 64-bit Release versions
BuildProject foo Win32 foo <-- builds 32-bit Release and Debug versions after source changes
BuildProject clean Win64 Release <-- cleans, then builds only 64-bit Release version
BuildProject foo foo foo <-- identical to BuildProject (with no arguments)
BuildProject clean foo Release <-- cleans, then builds 32- and 64-bit Release versions
BuildProject foo Win32 foo <-- builds 32-bit Release and Debug versions after source changes
BuildProject clean Win64 Release <-- cleans, then builds only 64-bit Release version
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:
@echo off
REM Clean, generate, and build Windows 32- and 64-bit debug and release
REM versions of the project DLLs and executables
REM Change for a different Visual Studio version. 12.0 = MSVC 2013, 14.0 = MSVC 2015, etc.
set vsversion=12.0
if "%1" == "clean" (
echo -- Cleaning
if EXIST install\libs\Windows rd /s /q install\libs\Windows
if EXIST install\Include rd /s /q install\Include
if EXIST build\Win32 rd /s /q build\Win32
if EXIST build\Win64 rd /s /q build\Win64
echo -- Cleaning done
echo --
)
set bitness=Win32 Win64
if "%2" == "Win32" (
set bitness=Win32
)
if "%2" == "Win64" (
set bitness=Win64
)
set build=Release Debug
if "%3" == "Release" (
set build=Release
)
if "%3" == "Debug" (
set build=Debug
)
REM This is required in order for "machine" and "arch" to be evaluated within the loops
setlocal EnableDelayedExpansion
for %%M in ( %build% ) do (
for %%W in ( %bitness% ) do (
REM and this ensures that any environment changes made by vcvarsall are scoped
REM by the inner loop
setlocal
if %%W == Win32 (
set machine=x86
set arch=x86
) else (
set machine=amd64
set arch=x64
)
echo -- Starting %%W %%M
if NOT EXIST build\%%W\%%M mkdir build\%%W\%%M
cd build\%%W\%%M
call "%ProgramFiles(x86)%\Microsoft Visual Studio %vsversion%\VC\vcvarsall" !machine!
echo -- Generating %%W %%M
cmake -G"NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake -DBUILD_TYPE=%%M -DTARGET_ARCH=!arch! ..\..\..
echo -- Building %%W %%M
cmake --build . 2>&1 >build.log
echo -- Installing %%W %%M
cmake -P cmake_install.cmake
echo -- Finished %%W %%M
cd ..\..\..
endlocal
)
)
@echo off
REM Clean, generate, and build Windows 32- and 64-bit debug and release
REM versions of the project DLLs and executables
REM Change for a different Visual Studio version. 12.0 = MSVC 2013, 14.0 = MSVC 2015, etc.
set vsversion=12.0
if "%1" == "clean" (
echo -- Cleaning
if EXIST install\libs\Windows rd /s /q install\libs\Windows
if EXIST install\Include rd /s /q install\Include
if EXIST build\Win32 rd /s /q build\Win32
if EXIST build\Win64 rd /s /q build\Win64
echo -- Cleaning done
echo --
)
set bitness=Win32 Win64
if "%2" == "Win32" (
set bitness=Win32
)
if "%2" == "Win64" (
set bitness=Win64
)
set build=Release Debug
if "%3" == "Release" (
set build=Release
)
if "%3" == "Debug" (
set build=Debug
)
REM This is required in order for "machine" and "arch" to be evaluated within the loops
setlocal EnableDelayedExpansion
for %%M in ( %build% ) do (
for %%W in ( %bitness% ) do (
REM and this ensures that any environment changes made by vcvarsall are scoped
REM by the inner loop
setlocal
if %%W == Win32 (
set machine=x86
set arch=x86
) else (
set machine=amd64
set arch=x64
)
echo -- Starting %%W %%M
if NOT EXIST build\%%W\%%M mkdir build\%%W\%%M
cd build\%%W\%%M
call "%ProgramFiles(x86)%\Microsoft Visual Studio %vsversion%\VC\vcvarsall" !machine!
echo -- Generating %%W %%M
cmake -G"NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake -DBUILD_TYPE=%%M -DTARGET_ARCH=!arch! ..\..\..
echo -- Building %%W %%M
cmake --build . 2>&1 >build.log
echo -- Installing %%W %%M
cmake -P cmake_install.cmake
echo -- Finished %%W %%M
cd ..\..\..
endlocal
)
)
To copy to clipboard, switch view to plain text mode
The windows_toolchain.cmake sets up some CMake options for building my particular project:
# target operating system
set (CMAKE_SYSTEM_NAME Windows)
set (WINDOWS true)
set (CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "Choose the type of build" FORCE)
set (CMAKE_CXX_FLAGS "/D WIN32 /EHsc /D UNICODE /D _UNICODE /D _WINDLL /D XERCES_STATIC_LIBRARY")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags" )
# target operating system
set (CMAKE_SYSTEM_NAME Windows)
set (WINDOWS true)
set (CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "Choose the type of build" FORCE)
set (CMAKE_CXX_FLAGS "/D WIN32 /EHsc /D UNICODE /D _UNICODE /D _WINDLL /D XERCES_STATIC_LIBRARY")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags" )
To copy to clipboard, switch view to plain text mode
Bookmarks