filipdutescu / modern-cpp-template

A template for modern C++ projects using CMake, Clang-Format, CI, unit testing and more, with support for downstream inclusion.
The Unlicense
1.73k stars 215 forks source link

[FEATURE] add AddressSanitizer as an option #41

Closed yunwei37 closed 1 year ago

yunwei37 commented 2 years ago

Is your feature request related to a problem? Please describe.

AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. see https://github.com/google/sanitizers/wiki/AddressSanitizer. The tool can detect the following types of bugs:

This tool is very fast. The average slowdown of the instrumented program is ~2x (see

And I think it's quite useful in C/C++ projects. You can simply enable it by compiling and linking your program with -fsanitize=address flag.

Describe the solution you'd like

Maybe we can add it as an option in cmake/StandardSettings.cmake ?

For example, like this:

option(${PROJECT_NAME}_ENABLE_ASAN "Enable Address Sanitize to detect memory error." ON)
if(${PROJECT_NAME}_ENABLE_ASAN)
  add_compile_options(-fsanitize=address)
  add_link_options(-fsanitize=address)
endif()
filipdutescu commented 1 year ago

That sounds great! Thanks for submitting the PR and hope we merge it ASAP :grin: