Build with debug infos, either -DCMAKE_BUILD_TYPE=RelWithDebInfo
or -DCMAKE_BUILD_TYPE=Debug
.
$ mkdir build
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -B build
$ cmake --build build
PS> mkdir build
PS> cmake -DCMAKE_BUILD_TYPE=Debug -B build
PS> cmake --build build --config Debug
To use clang-tidy
pass USE_CLANG_TIDY=ON
to CMake.
$ cmake -DUSE_CLANG_TIDY=ON [..]
$ cmake --build .
If clang-tidy
is installed under a different name pass that name or the full path with the CMAKE_CXX_CLANG_TIDY
option like:
$ cmake -DUSE_CLANG_TIDY=ON -DCMAKE_CXX_CLANG_TIDY=/opt/local/bin/clang-tidy-11 [..]
$ cmake --build .
To use sanitizers pass the USE_SANITIZER=[..]
option to CMake with one or more of the following values (case insensitive; multiple options delimited by comma or space):
address
orASAN
leak
orLSAN
memory
orMSAN
thread
orTSAN
undefined
orUBSAN
Note: Make sure to build with either -DCMAKE_BUILD_TYPE=RelWithDebInfo
or -DCMAKE_BUILD_TYPE=Debug
or to set -g
compiler flag manually.
address
orASAN
Note: For MSVC make sure to build with either -DCMAKE_BUILD_TYPE=RelWithDebInfo
or -DCMAKE_BUILD_TYPE=Debug
or to set /Zi
compiler flag manually.
Clang-cl does not (yet) support linking with /MDd
, so use RelWithDebInfo
instead of Debug
.
https://github.com/google/sanitizers/wiki/AddressSanitizer
Builds the project targets with -fsanitize=address -fno-omit-frame-pointer
(GCC, Clang) or /fsanitize=address
(MSVC).
This automatically enables LeakSanitizer on Linux. To enable it on macOS pass the ASAN_OPTIONS=detect_leaks=1
flag when running a program.
$ cmake -DUSE_SANITIZER=address [..]
$ cmake --build .
Some checks are only activated after passing a run-time flag:
asan_initialization-order-fiasco
: Run with either:
ASAN_OPTIONS=check_initialization_order=1
ASAN_OPTIONS=check_initialization_order=1:strict_init_order=1
asan_memory-leak_malloc
asan_memory-leak_new
: Need to run withASAN_OPTIONS=detect_leaks=1
on macOS.asan_stack-use-after-return
: Need to run withASAN_OPTIONS=detect_stack_use_after_return=1
.
https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
Builds the project targets with -fsanitize=leak
.
LeakSanitizer is automatically included in AddressSanitizer. This just uses the stand-alone version without the AddressSanitizer overhead.
To use it on macOS run the programs with the ASAN_OPTIONS=detect_leaks=1
flag.
$ cmake -DUSE_SANITIZER=leak [..]
$ cmake --build .
https://github.com/google/sanitizers/wiki/MemorySanitizer
Builds the project targets with -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer -fPIE -pie
.
$ cmake -DUSE_SANITIZER=memory [..]
$ cmake --build .
https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
Builds the project targets with -fsanitize=thread
.
$ cmake -DUSE_SANITIZER=thread [..]
$ cmake --build .
https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
Builds the project targets with -fsanitize=undefined -fno-omit-frame-pointer
.
Run the programs with the flag UBSAN_OPTIONS=print_stacktrace=1
to generate stack traces.
$ cmake -DUSE_SANITIZER=undefined [..]
$ cmake --build .