Correctly setting address sanitizer for usage with clang

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

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

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

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.

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

1 Like

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.

Probably this one on SO. 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).

1 Like

Thank you for the pointers.
Regards
A.