The Visual Studio project generated by CMake defaults to the Debug build type, as shown in the image below:
Sometimes, you may want to specify the build type in CMake without having to manually adjust it every time you open the project.
1. General Settings
1.1 Specifying in CMakeLists.txt
This can be achieved by setting the variable CMAKE_BUILD_TYPE
.
cmake_minimum_required(VERSION 3.0)
project(HelloWorld)
set(CMAKE_BUILD_TYPE Release)
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
add_executable(HelloWorld main.cpp)
1.2 Notes
- The setting of
<span>CMAKE_BUILD_TYPE</span>
should be done before adding the target.
1.3 Specifying at CMake Run Time
cmake ../source_dir -DCMAKE_BUILD_TYPE=Release
1.4 Recommended Usage
To prevent the CMakeLists.txt
from overriding the runtime specification, it is recommended to use the following in CMakeLists.txt
:
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
The complete example is as follows:
cmake_minimum_required(VERSION 3.0)
project(HelloWorld)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
add_executable(HelloWorld main.cpp)
1.5 Build Types
- Debug
- Release
- RelWithDebInfo
- MinSizeRel
2. Configuration for Visual Studio
The above modifications do not change the Visual Studio configuration; to modify the Visual Studio configuration, you need to set the variable <span>CMAKE_CONFIGURATION_TYPES</span>
.
The core statement is as follows:
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
The complete example is as follows:
cmake_minimum_required(VERSION 3.0)
project(HelloWorld)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
add_executable(HelloWorld main.cpp)
The effect after opening is as follows:
A drawback of the above configuration is that the configuration only has “Release”. If you need additional types, you can modify it as follows, adding a “RelWithDebInfo” type. If you want to add more types, just append them:
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" "RelWithDebInfo")
endif()
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
After testing, it was found that replacing <span>SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)</span>
with <span>SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE})</span>
is also feasible.
3. Generating Programs Without Opening Visual Studio
If you only want to generate the <span>Release</span>
version of the program without opening the Visual Studio project, you can use the following commands to generate the program:
cmake -G "Visual Studio 14 Win64" ..
cmake --build . --target ALL_BUILD --config Release
After execution, the Release version of the program will be generated in the Release folder.