title: What's the proper way to enable AddressSanitizer in CMake that works in Xcode
body: <p>I've added AddressSanitizer flag as follow:</p>
  
  <pre><code>set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
  </code></pre>
  
  <p>Everything builds and runs fine when using <code>Unix Makefiles</code>.</p>
  
  <p>The problem comes when generating the Xcode project, it just doesn't want to link because it cannot find the ASan library.</p>
  
  <p>I already found two solutions, but decided not to use them because they cannot be automated using just CMake:</p>
  
  <ol>
  <li>Adding <code>-Wl,-undefined,dynamic_lookup</code> to the linked flags, so it skips linking to dynamic libraries. </li>
  <li>Link with <code>libclang_rt.asan_osx_dynamic.dylib</code> directly.</li>
  </ol>
  
  <p>So what's the problem with these two solutions?</p>
  
  <ul>
  <li>When using solution #1, I have to manually open the target scheme in Xcode and add <code>DYLD_INSERT_LIBRARIES</code> environment variable pointing to <code>libclang_rt.asan_osx_dynamic.dylib</code>.</li>
  <li>When using solution #2, the path for the ASan library varies between computers.</li>
  </ul>
  
  <p>Additionally as another solution, I tried enabling Address Sanitizer flag from the Xcode target scheme but interestingly it didn't detect the issues I added, so I didn't list this as a solution because it failed my test.</p>
  
  <p>Any help will be much appreciated.</p>