11

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
iseale
  • 111
  • 1
  • 4
  • My version of clang is a brew installation. Do I need to build clang from scratch in order to enable sanitizers? – iseale Oct 30 '17 at 20:34

1 Answers1

11

Solution from here

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

-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"

Yunkang YU
  • 111
  • 1
  • 5
  • BTW, Apple Clang (the one that comes with Xcode) not supports fsanitize – Yunkang YU Mar 22 '19 at 12:22
  • 3
    Just a note. I had spent hours and hours to figure this out. Even though it is clearly mentioned in the example - https://clang.llvm.org/docs/AddressSanitizer.html#how-to-build. Elegant solution to set the flag in cmake will be : *string(APPEND CMAKE_EXE_LINKER_FLAGS "-fsanitize=address")* – irsis May 24 '20 at 12:01
  • A more modern CMake solution: `add_link_options(-fsanitize=address)` https://cmake.org/cmake/help/latest/command/add_link_options.html – Flash Sheridan Mar 10 '23 at 23:25