Getting ready

In this example, we will use two source files and two tests, as follows:

.├── CMakeLists.txt├── CTestConfig.cmake├── dashboard.cmake├── src│   ├── buggy.cpp│   ├── buggy.hpp│   └── CMakeLists.txt└── tests    ├── CMakeLists.txt    ├── leaky.cpp    └── use_after_free.cpp

The file buggy.cpp contains two buggy functions, as follows:

#include "buggy.hpp"#include <iostream>int function_leaky() {  double *my_array = new double[1000];  // do some work ...  // we forget to deallocate the array  // delete[] my_array;  return 0;}int function_use_after_free() {  double *another_array = new double[1000];  // do some work ...  // deallocate it, good!  delete[] another_array;  // however, we accidentally use the array  // after it has been deallocated std::cout << ...

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.