How to do it

In this recipe, we will show how the build type can be set for an example project:

  1. We start out by defining the minimum CMake version, project name, and supported languages:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-07 LANGUAGES C CXX)
  1. Then, we set a default build type (in this case, Release) and print it in a message for the user. Note that the variable is set as a CACHE variable, so that it can be subsequently edited through the cache:
if(NOT CMAKE_BUILD_TYPE)  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)endif()message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  1. Finally, we print corresponding compile flags set by CMake as a function of the build type:
message(STATUS "C flags, Debug configuration: ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.