title: Link problems with libc++abi when linking against libc++ via cmake
body: <p>I'm trying to build a simple ("hello world") <code>C++</code> program with LLVM/Clang 3.7.0 built from sources against the toolchain's <code>libc++</code>, with the command line:</p>
  
  <pre><code>clang++ -std=c++14 -stdlib=libc++ -fno-exceptions hello.cpp
  </code></pre>
  
  <p>However, I get the following errors:</p>
  
  <pre><code>/usr/bin/ld: warning: libc++abi.so.1, needed by /bulk/workbench/llvm/3.7.0
  /toolchain4/bin/../lib/libc++.so, not found (try using -rpath or -rpath-link)
  /bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `__cxa_rethrow_primary_exception'
  /bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `__cxa_decrement_exception_refcount'
  /bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `std::out_of_range::~out_of_range()'
  [...]
  </code></pre>
  
  <p>The <code>LD_LIBRARY_PATH</code> is <em>not</em> set and the toolchain's install directory is added to my working <code>PATH</code> by:</p>
  
  <pre><code>export PATH=$PATH:/bulk/workbench/llvm/3.7.0/toolchain4/bin/
  </code></pre>
  
  <p>I'm on <code>Ubuntu GNU/Linux 14.04</code> and I have not installed anything LLVM or Clang related packages from any repository.</p>
  
  <p>According to the <a href="http://libcxx.llvm.org/docs/UsingLibcxx.html#using-libc-on-linux" rel="nofollow noreferrer">libc++ documentation</a>:</p>
  
  <blockquote>
    <p>On Linux libc++ can typically be used with only ‘-stdlib=libc++’. However some libc++ installations require the user manually link libc++abi themselves. If you are running into linker errors when using libc++ try adding ‘-lc++abi’ to the link line.</p>
  </blockquote>
  
  <p>Doing as suggested gives a successful build.</p>
  
  <p><em>So, my question is this:</em></p>
  
  <p>Why do I have to specify the <code>-lc++abi</code> dependency explicitly on the line of the build command?</p>
  
  <p>Doing</p>
  
  <pre><code>readelf -d $(llvm-config --libdir)/libc++.so
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>Dynamic section at offset 0xb68c8 contains 31 entries:
    Tag        Type                         Name/Value
   0x0000000000000001 (NEEDED)             Shared library: [libc++abi.so.1]
   0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
   0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
   0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
   0x000000000000000e (SONAME)             Library soname: [libc++.so.1]
   0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/../lib]
   0x000000000000000c (INIT)               0x350a8
  [...]
  </code></pre>
  
  <p>Shouldn't the embedded <code>RPATH</code> in the dynamic section of the ELF be considered by <code>ld</code> as described in its <a href="http://linux.die.net/man/1/ld" rel="nofollow noreferrer">man page</a> under the section <code>-rpath-link=dir</code>?</p>
  
  <p>Moreover, when I set the <code>LD_LIBRARY_PATH</code> with</p>
  
  <pre><code>LD_LIBRARY_PATH=$(llvm-config --libdir)
  </code></pre>
  
  <p>the initial build command (without specifying <code>-lc++abi</code>) works, as also described in the 5th clause of the aforementioned man entry.</p>