Turn Address Sanitizer when available

Hi, I am trying to create an option in my CMake builds that turns on Address Sanitizer when selected.

I found this for Xcode:

set(CMAKE_XCODE_GENERATE_SCHEME ON)
set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER ON)  set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN ON)

Are there other flags for other generators? Is there a flag that works with all generators? Ideally I need something for MSVC, GCC and Clang.

1 Like

I don’t know of anything for other generators. Those flags basically just tell Xcode to do things whereas other generators need CMake code to add the flags manually. Something like this is what I do:

It is split into two separate files that are included so that things like try_compile and such don’t inherit the sanitizer flags (which tend to cause false-negative detections of features due to poorly-coded detection snippets triggering the sanitizers).

Did you have a look at: GitHub - arsenm/sanitizers-cmake: CMake modules to help use sanitizers
I use it for some time now and the module does a pretty good job for what I need (with GCC and Clang at least).

1 Like

Thanks, I didn’t knew this!

Edit: now I see this hasn’t been updated on the last three years. :confused: