# An example CMakeLists.txt for the sanitizers example, showing how to # enable sanitizers in a debug build. # It uses generator expressions, to set additional flags when the build type # is Debug. # # To try this out, first create a build directory for a release build, # and do a release build, e.g., # % mkdir build-rel # % cd build-rel # % cmake ../sanitizers -DCMAKE_BUILD_TYPE=Release # % make # # Run the examples and see that they crash. # # Then create another build directory and do a debug build: # # % mkdir build-dbg # % cd build-dbg # % cmake ../sanitizers -DCMAKE_BUILD_TYPE=Debug # % make # # Run the examples and verify that the sanitizers find the errors. # # If you want to see the actual commands run during the build, for instance # to verify that the correct compiler flags are used, you can do # # % make VERBOSE=1 cmake_minimum_required (VERSION 3.5) project (Sanitizers) set (CMAKE_CXX_STANDARD 11) # The three standard build types are Release, Debug, and RelWithDebInfo. # If set here, it forces that build type. # # set(CMAKE_BUILD_TYPE Release) # set build type to DEBUG # set(CMAKE_BUILD_TYPE Debug) # or to get an optimized build w/ debug symbols # set(CMAKE_BUILD_TYPE RelWithDebInfo) # add the executables add_executable(leak leak.cc) add_executable(bounds bounds.cc) add_executable(ub ub.cc) add_executable(dangling dangling.cc) # set compiler flag to turn off optimization (for all builds) # add_compile_options("-O0") # or use generator expressions to set flags in debug builds add_compile_options($<$:-O0>) # set compiler and linker flags to enable the relevant sanitizer target_compile_options(leak PUBLIC $<$:-fsanitize=leak>) target_link_libraries(leak $<$:-fsanitize=leak>) target_compile_options(bounds PUBLIC $<$:-fsanitize=address>) target_link_libraries(bounds $<$:-fsanitize=address>) target_compile_options(ub PUBLIC $<$:-fsanitize=undefined>) target_link_libraries(ub $<$:-fsanitize=undefined>) target_compile_options(dangling PUBLIC $<$:-fsanitize=address>) target_link_libraries(dangling $<$:-fsanitize=address>)