# Correctly setting address sanitizer for usage with clang

**URL:** https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296
**Category:** Usage
**Tags:** os:linux
**Created:** [January 20, 2023, 4:11pm UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296 "2023-01-20T16:11:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 20, 2023, 4:11pm UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/1 "2023-01-20T16:11:12Z")

</div>

Hi, I’m doing this but, either if it seems to work, I’m not sure it’s the proper way to activate sanitizing with clang

```cmake
set(SANITIZER_FLAGS -fsanitize=address,undefined,float-divide-by-zero,nullability -fno-omit-frame-pointer)
target_compile_options(${TARGET_NAME} PRIVATE "${SANITIZER_FLAGS}")
target_link_options(${TARGET_NAME} PRIVATE "${SANITIZER_FLAGS}")

```

I thought that I should write

```cmake
target_link_options(${TARGET_NAME} PRIVATE "LINKER:${SANITIZER_FLAGS}")

```

but then I’ve got a linker error from ld.

By the way, I think that I should not be calling ld.

What is the proper way to set the right linker for a given compiler (may be it should be another topic)?

Thanks

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [January 21, 2023, 12:54am UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/2 "2023-01-21T00:54:15Z")

</div>

The compiler driver is used as compiler and linker, in your case clang/clang++.  
These options are clang specific, so they are consumed by that executable. It then calls `ld` from GNU Binutils under the hood with the right options to link additional libraries.  
With `LINKER:` you specify that the options shall be given to the underlying linker program instead, which usually has a limited/different set of options.

---

<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: [January 22, 2023, 2:39am UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/3 "2023-01-22T02:39:18Z")

</div>

`-fsanitize=` is for the frontend, never the linker. It turns into the right flags for the compiler when compiling and the linker when linking (mainly the appropriate runtime library there).

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 23, 2023, 4:38pm UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/4 "2023-01-23T16:38:07Z")

</div>

Thanks to both of you for the feedback.

Unfortunaly I’m not fluent enough in “compilation” wording to understand your answers (except that I must not write the “LINKER:”).

By the way, would you mind pointing me a “compilation for dummies” resource that may be of some help to understand your comments above (especially the difference frontend/linker)?

Regards  
A.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [January 23, 2023, 6:57pm UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/5 "2023-01-23T18:57:55Z")

</div>

Probably this one on [SO](https://stackoverflow.com/questions/32595607/difference-between-compiler-and-compiler-driver-in-llvm). Or search for words like “compiler driver linker”.

In short:  
If you simply call `gcc main.cpp` or `clang main.cpp`, they will:

- preprocess
- parse code to some AST (abstract syntax tree)
- generate assembler for specific CPU
- create object file
- link to executable

Each of these steps may be done by a different executable with a different command line interface.  
You normally don’t (want to) know these programs.  
Only for special use cases, like some embedded cross compilers, you may need a special linker flag, that the compiler driver does not expose in its command line.

These compiler drivers have options like `-c` to stop after creating an object file. That’s what all build tools usually do, to provide incremental builds (only recompile source files that have changed).

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 25, 2023, 2:04pm UTC](https://discourse.cmake.org/t/correctly-setting-address-sanitizer-for-usage-with-clang/7296/6 "2023-01-25T14:04:43Z")

</div>

Thank you for the pointers.  
Regards  
A.
