# using /fsanitize=address option

**URL:** https://discourse.cmake.org/t/using-fsanitize-address-option/6976
**Category:** Usage
**Tags:** os:windows
**Created:** [November 28, 2022, 5:30pm UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976 "2022-11-28T17:30:19Z")
**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: [November 28, 2022, 5:30pm UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/1 "2022-11-28T17:30:19Z")

</div>

Hi

When compiling project with msvc cl, I use the NMake makefiles generator. Though, if adding /fsanitize=address option to my compilation and linking option. My project compile but does nothing when it runs (it displays neither its normal output, nor a sanitizer output).  
When I directly compile my sample program with cl main.cpp /fsanitize=address /Zi though, I’ve got an output and a report.

Sorry, so far I cannot propose a simple standalone example.

Regards  
A.

---

<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: [November 30, 2022, 7:56pm UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/2 "2022-11-30T19:56:05Z")

</div>

How are you adding the flag to the command line in CMake code? Can you show the command that actually gets executed as well?

---

<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: [December 1, 2022, 8:10am UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/3 "2022-12-01T08:10:44Z")

</div>

Hi, thanks for your feedback

```cmake
#MY_COMPIL_FLAGS already contains some options
set(MY_COMPIL_FLAGS ${MY_COMPIL_FLAGS} /fsanitize=address)
set(CMAKE_EXE_LINKER_FLAGS "/fsanitize=address")
target_compile_options(${TARGET_NAME} PRIVATE ${MY_COMPIL_FLAGS})

```

With my NMake Makefile generator, I cannot issue the exact executed command. Though, the CMakeFiles\Param.dir\flags.make file is containing (for a debug-like configuration):

```cmake
CXX_FLAGS = /DWIN32 /D_WINDOWS /EHsc -MDd /DWIN32 /D_WINDOWS /GR /EHsc /MP /Zc:__cplusplus /permissive- /MDd /Ob0 /Zi /RTC1 /Od /Wall /fsanitize=address

```

for a release-like conf:

```cmake
CXX_FLAGS = /DWIN32 /D_WINDOWS /EHsc -MD /GL /DWIN32 /D_WINDOWS /GR /EHsc /MP /Zc:__cplusplus /permissive- /MD /Ob2 /DNDEBUG /O2 /Wall /fsanitize=address

```

NB: you may observe that some parameters are duplicated whereas I first empty CMAKE\_CXX\_FLAGS\_RELEASE and CMAKE\_CXX\_FLAGS\_DEBUG to avoid some default parameters set by cmake.

I’m wondering may I forget to explicitly set linker options with target\_link\_options?  
I already set the langage with

```cmake
# CPPLANG may be -std=c++17, for instance
target_link_options(${TARGET_NAME} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:${CPPLANG}>)

```

What would be the correct way to append another option? I understand that the command is appending args at each call, Am I correct?

NB again: I cannot find, in my cmake-generated file, where are the linker options? Though they seem to be set (at least the langage used…)

regards  
A.

---

<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: [December 1, 2022, 8:45am UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/4 "2022-12-01T08:45:35Z")

</div>

Ahah,

VSCode output was misleading. When executing my sample in its embedded terminal it silenced an error: “clang\_rt.asan\_dynamic-x86\_64.dll” could not be found  
whereas cl main.cpp /fsanitize=address /Zi works.

Through cmake, it seems that the asan library is not linked properly

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [December 1, 2022, 8:58am UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/5 "2022-12-01T08:58:12Z")

</div>

The key is the environment. In the visual studio command prompt, the asan library is in the path. If you run that executable outside of that command prompt, it will also not work unless you copy the asan DLL.

Basic Windows madness.

---

<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: [December 1, 2022, 10:27am UTC](https://discourse.cmake.org/t/using-fsanitize-address-option/6976/6 "2022-12-01T10:27:53Z")

</div>

Indeed I forgot to reset the vs environment in my external terminal !

Thus the statut is: sanitize option is correctly set, even with cmake (though I still don’t know if I do it properly, see my "sub-"questions above.

The vs environment seem not to be set properly in vscode powershell embedded terminal (environment vars are not the same as in a “normal” terminal, even if I launch the same scripts for setting msvc environment)

strange enough because, otherwise I can compile with msvc and run the binary within the embedded terminal but not with sanitize option.

Seems to be a vscode or a powershel issue, not a cmake one.  
Note I launch my cmake and compilation commands within a python script that also sets the environment variables. But when I leave the script, it seems that the environment is reset to its previous state: for instance “cl” command is not found anymore…

I can close the subject and dive in environment variable persistence and so on…

Thanks for having pinpointed the issue  
A.
