Build types in CMake

A build type indicates a set of compile-time decisions used while compiling your code. For example, whether or not to optimize or to include debug information. A build type or build mode or configuration is popular in Visual Studio, where C++ projects typically have at least two types: Release and Debug.

CMake has build types and follows similar rules. For compiling C++, there are at least 4 build types in CMake: Release, Debug, MinSizeRel and RelWithDebInfo.

The compile options or rules associated with a build type for a source file of a particular language can be found in /usr/share/cmake-2.8/Modules/Find*.cmake. For compiled languages like C++, look for the matching compiler in /usr/share/cmake-2.8/Modules/Compiler directory.

For C++ code compiled using GNU C++ compiler, see the files /usr/share/cmake-2.8/Modules/Compiler/GNU.cmake for the options used during compilation.

When no build type is provided, I found the compilation string to be: g++ /your/code.cpp

For Release build type, I found the compilation string to be: g++ -O3 -DNDEBUG /your/code.cpp. Note that assert will be disabled with this build type.

For Debug build type, I found the compilation string to be: g++ -g /your/code.cpp

You can specify a build type in the CMakeLists.txt file. For example:

set(CMAKE_BUILD_TYPE Release)

You can also specify a build type at the shell. For example:

$ cmake -D CMAKE_BUILD_TYPE=Debug ..

You can make one of the build types as default, when no build type is provided. For example, to make Release build type as default, add this to CMakeLists.txt: