How to do it

We need to compile our code with particular flags to take advantage of ASan. Then, we will run tests and submit them to the dashboard. Let us take a look at how to do this:

  1. The buggy library is defined in src/CMakeLists.txt:
add_library(buggy "")target_sources(buggy  PRIVATE    buggy.cpp  PUBLIC    ${CMAKE_CURRENT_LIST_DIR}/buggy.hpp  )target_include_directories(buggy  PUBLIC    ${CMAKE_CURRENT_LIST_DIR}  )
  1. To the file src/CMakeLists.txt, we will add an option and code to sanitize using ASan:
option(ENABLE_ASAN "Enable AddressSanitizer" OFF)if(ENABLE_ASAN)  if(CMAKE_CXX_COMPILER_ID MATCHES GNU)    message(STATUS "AddressSanitizer enabled")    target_compile_options(buggy      PUBLIC        -g -O1 -fsanitize=address -fno-omit-frame-pointer      ) target_link_libraries(buggy ...

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.