How to do it

The following shows how to set up CMakeLists.txt to perform the dynamic analysis of the code:

  1. We first define the minimum CMake version, project name, language, targets, and dependencies:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-05 LANGUAGES CXX)set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)add_library(example_library leaky_implementation.cpp)
add_executable(cpp_test test.cpp)target_link_libraries(cpp_test example_library)
  1. Then, we define not only the test, but also the MEMORYCHECK_COMMAND:
find_program(MEMORYCHECK_COMMAND NAMES valgrind)set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")# add memcheck test actioninclude(CTest)enable_testing() ...

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.