# Turn Address Sanitizer when available

**URL:** <https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891>\
**Category:** Code\
**Created:** [August 9, 2021, 10:49am UTC](https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891 "2021-08-09T10:49:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eri0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eri0/32/733_2.png) [@eri0](https://discourse.cmake.org/u/eri0)\
**Post date:** [August 9, 2021, 10:49am UTC](https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891/1 "2021-08-09T10:49:04Z")

</div>

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

I found this for Xcode:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 9, 2021, 11:30am UTC](https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891/2 "2021-08-09T11:30:34Z")

</div>

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:

- [options for users to set](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/vtkSanitize.cmake)
- [actually using the flags](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/vtkSanitizeFlags.cmake)

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).

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [August 9, 2021, 1:05pm UTC](https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891/3 "2021-08-09T13:05:18Z")

</div>

> [@eri0](#):
>
> 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.

Did you have a look at: [GitHub - arsenm/sanitizers-cmake: CMake modules to help use sanitizers](https://github.com/arsenm/sanitizers-cmake)  
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).

---

<div class="post-metadata">

**Author:** ![eri0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eri0/32/733_2.png) [@eri0](https://discourse.cmake.org/u/eri0)\
**Post date:** [August 11, 2021, 3:31pm UTC](https://discourse.cmake.org/t/turn-address-sanitizer-when-available/3891/4 "2021-08-11T15:31:01Z")

</div>

Thanks, I didn’t knew this!

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