Skip to content

Instantly share code, notes, and snippets.

@jlblancoc
Last active February 16, 2023 15:45
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jlblancoc/44be9d4d466f0a973b1f3808a8e56782 to your computer and use it in GitHub Desktop.
Save jlblancoc/44be9d4d466f0a973b1f3808a8e56782 to your computer and use it in GitHub Desktop.
GCC sanitizer with CMake

For memory leaks

Build in CMake with these params:

CMAKE_CXX_FLAGS:STRING= -fsanitize=address  -fsanitize=leak -g
CMAKE_C_FLAGS:STRING=-fsanitize=address  -fsanitize=leak -g
CMAKE_EXE_LINKER_FLAGS:STRING=-fsanitize=address  -fsanitize=leak
CMAKE_MODULE_LINKER_FLAGS:STRING=-fsanitize=address  -fsanitize=leak

Which can be done with:

cmake .. -DCMAKE_CXX_FLAGS="-fsanitize=address  -fsanitize=leak -g" -DCMAKE_C_FLAGS="-fsanitize=address  -fsanitize=leak -g" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address  -fsanitize=leak" -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=address  -fsanitize=leak"

Then run with:

export ASAN_OPTIONS=fast_unwind_on_malloc=0

For multithreading issues

Which can be done with:

cmake .. -DCMAKE_CXX_FLAGS="-fsanitize=thread -g" -DCMAKE_C_FLAGS="-fsanitize=thread -g" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread" -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=thread"
@cryptid11
Copy link

these seems to not work if i have a Makefile instead of a CMakeLists.txt.
I think this should work tought if you put this at the beginning of the file: (deny/confirm?)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak -g")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment