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:

  • 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:
    set(CMAKE_BUILD_TYPE Release)

Note: Build types for Visual Studio cannot be set in CMakeLists.txt using CMAKE_BUILD_TYPE or any other method. It can only be set when CMake is invoked or later in the Visual Studio solution.

Tried with: CMake and Ubuntu 14.04

2 thoughts on “Build types in CMake

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.