Sanitizers Add sanitizer flag to all targets 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15include(CheckCXXCompilerFlag) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") message("Looking for -fsanitize=${flag}") set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=${flag}") check_cxx_compiler_flag(-fsanitize=${flag} HAVE_FLAG_SANITIZER) if (HAVE_FLAG_SANITIZER) message("Adding -fsanitize=${flag}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${flag} -fno-omit-frame-pointer") set(DCMAKE_C_FLAGS "${DCMAKE_C_FLAGS} -fsanitize=${flag} -fno-omit-frame-pointer") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${flag}") set(DCMAKE_MODULE_LINKER_FLAGS "${DCMAKE_MODULE_LINKER_FLAGS} -fsanitize=${flag}") else () message("-fsanitize=${flag} unavailable") endif () endif () Add address sanitizer to all targets 1add_sanitizer("address") Add thread sanitizer to all targets 1add_sanitizer("thread") Add undefined sanitizer to all targets 1add_sanitizer("undefined") Add memory sanitizer to all targets 1add_sanitizer("memory") Add leak sanitizer to all targets 1add_sanitizer("leak") Add all sanitizers to all targets 1 2 3 4 5 6 7 8# Choose a subset of sanitizers not in conflict add_address_sanitizer() add_leak_sanitizer() add_undefined_sanitizer() # not allowed with address sanitizer # add_thread_sanitizer() # not supported # add_memory_sanitizer() Share Snippets