6
votes

I have been trying to use Clang's address code sanitizer, but the linker will not have it. The linker may be "ld", though my CMAKE settings assure me that clang is the linker.

Added -fsanitize=address to compiler and linker flags.

Error:

Undefined symbols for architecture x86_64:
___asan_after_dynamic_init
...
___asan_before_dynamic_init
...
etc.
ld: symbol(s) not found for architecture x86_64    <<<< **suspicious**
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • Environment: MacOS
  • clang: Apple LLVM version 8.0.0 (clang-800.0.38)
  • cmake: 3.7.1

  • CMAKE_CXX_COMPILER = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (redirects to clang)

  • CMAKE_CXX_COMPILER_ID = Clang
  • Compiler Flags: -O0 -g -fsanitize=address
  • CMAKE_LINKER = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (redirects to clang)
  • CMAKE_CXX_LINK_EXECUTABLE = CMAKE_CXX_COMPILER FLAGS CMAKE_CXX_LINK_FLAGS LINK_FLAGS OBJECTS -o TARGET LINK_LIBRARIES
  • CMAKE_CXX_LINK_FLAGS = -Wl,-search_paths_first -Wl,-headerpad_max_install_names -fsanitize=address -v
1
My version of clang is a brew installation. Do I need to build clang from scratch in order to enable sanitizers?iseale

1 Answers

5
votes

Solution from here

Pass the -fsanitize=address flag to the linker as well as the compiler.

-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"