I am trying to run a simple leak check program.

#include <iostream> int main() { double *ptr = new double(3.14); } 

using the command g++ -g -fsanitize=leak -o main main.cpp

and I get the following error:

clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'

I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.

$ which clang++ /usr/local/opt/llvm/bin/clang++ 
clang++ --version clang version 11.0.0 Target: x86_64-apple-darwin20.1.0 Thread model: posix InstalledDir: /usr/local/opt/llvm/bin 

EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to @cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.

 g++ --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/ Apple clang version 12.0.0 (clang-1200.0.32.27) Target: x86_64-apple-darwin20.1.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin 
4

1 Answer

According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.

  • Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10 > /usr/local/bin/g++-10 -- $ which g++ > /usr/bin/g++ //this is pseudonym of clang 

The same for gcc-10 (10 is my current version. You should use your version instead of that)


If you use CMakeLists.txt file you will configure it like this:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]") # leak sanitizer_name not works for me. should be address 

And should execute cmake command like this: cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 .. And then ASAN_OPTIONS=detect_leaks=1 ./a.out

Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy