How to do it

Sanitizers have been available for a while with the Clang compiler and were later also introduced into the GCC toolset. They were designed for use with C and C++ programs, but recent versions of Fortran will understand the same flags and produce correctly instrumented libraries and executables. This recipe will however focus on a C++ example.

  1. As usual, we first declare a C++11 project:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-07 LANGUAGES CXX)set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. We declare a list, CXX_BASIC_FLAGS, containing the compiler flags to be always used when building the project, -g3 and -O1:
list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")

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.