using /fsanitize=address option


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.


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

Hi, thanks for your feedback

#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):

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:

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

# CPPLANG may be -std=c++17, for instance

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



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

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.

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