Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

How to Build C++ Projects with the Address Sanitizer on Windows

memory bug detection via compiler extension

clang-cl -fsanitize=address main.cpp
clang-cl -fsanitize=address -c main.cpp -o main.o
# Call vcvars64 here so lld-link will be able to link all the default libraries
lld-link main.o -libpath:"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\lib\clang\10.0.0\lib\windows" ^
    -wholearchive:clang_rt.asan-x86_64.lib -wholearchive:clang_rt.asan_cxx-x86_64.lib
clang-cl -fsanitize=address /MD main.cpp
clang-cl -fsanitize=address -c main.cpp -o main.o
# Call vcvars64 here so all the default libraries are added to lld-link
lld-link main.o -libpath:"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\lib\clang\10.0.0\lib\windows" ^
    clang_rt.asan_dynamic-x86_64.lib -wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib
add_executable(exec main.cpp)
target_compile_options(exec PRIVATE -fsanitize=address) # /MD will be used implicitly
target_link_directories(exec PRIVATE "${ENV:PROGRAMFILES\(X86\)}/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/lib/clang/10.0.0/lib/windows")
target_link_libraries(exec PRIVATE clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64)
target_link_options(exec PRIVATE /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib)

About KDAB


5 Comments

23 - Sept - 2020

Markus H

14 - Oct - 2020

Alessandro Ambrosano

2 - Feb - 2021

Sören

29 - Nov - 2022

Alessandro Ambrosano

30 - Nov - 2022

Alex G

AlessandorAmbrosano

Alessandro Ambrosano

Senior Software Engineer

Learn Modern C++

Learn more