通过 cmake 链接 libc++ 时 libc++abi 的链接问题

com*_*por 6 linker abi llvm clang libc++

我正在尝试C++使用 LLVM/Clang 3.7.0 构建一个简单的(“hello world”)程序,该程序是根据工具链的源代码构建的libc++,使用命令行:

\n\n
clang++ -std=c++14 -stdlib=libc++ -fno-exceptions hello.cpp\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,我收到以下错误:

\n\n
/usr/bin/ld: warning: libc++abi.so.1, needed by /bulk/workbench/llvm/3.7.0\n/toolchain4/bin/../lib/libc++.so, not found (try using -rpath or -rpath-link)\n/bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `__cxa_rethrow_primary_exception\'\n/bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `__cxa_decrement_exception_refcount\'\n/bulk/workbench/llvm/3.7.0/toolchain4/bin/../lib/libc++.so: undefined reference to `std::out_of_range::~out_of_range()\'\n[...]\n
Run Code Online (Sandbox Code Playgroud)\n\n

LD_LIBRARY_PATH设置工具链的安装目录已添加到我的工作中PATH中:

\n\n
export PATH=$PATH:/bulk/workbench/llvm/3.7.0/toolchain4/bin/\n
Run Code Online (Sandbox Code Playgroud)\n\n

我上线了Ubuntu GNU/Linux 14.04,并且尚未从任何存储库安装任何 LLVM 或 Clang 相关的软件包。

\n\n

根据libc++ 文档

\n\n
\n

在 Linux 上,libc++ 通常只能与 \xe2\x80\x98-stdlib=libc++\xe2\x80\x99 一起使用。然而,某些 libc++ 安装需要用户自己手动链接 libc++abi。如果在使用 libc++ 时遇到链接器错误,请尝试将 \xe2\x80\x98-lc++abi\xe2\x80\x99 添加到链接行。

\n
\n\n

按照建议进行操作即可成功构建。

\n\n

所以,我的问题是这样的:

\n\n

为什么我必须-lc++abi在构建命令行上显式指定依赖项?

\n\n

正在做

\n\n
readelf -d $(llvm-config --libdir)/libc++.so\n
Run Code Online (Sandbox Code Playgroud)\n\n

给出

\n\n
Dynamic section at offset 0xb68c8 contains 31 entries:\n  Tag        Type                         Name/Value\n 0x0000000000000001 (NEEDED)             Shared library: [libc++abi.so.1]\n 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]\n 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]\n 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]\n 0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]\n 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]\n 0x000000000000000e (SONAME)             Library soname: [libc++.so.1]\n 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/../lib]\n 0x000000000000000c (INIT)               0x350a8\n[...]\n
Run Code Online (Sandbox Code Playgroud)\n\n

难道不应该按照其手册页中部分下的描述来RPATH考虑嵌入 ELF 的动态部分吗?ld-rpath-link=dir

\n\n

此外,当我LD_LIBRARY_PATH设置

\n\n
LD_LIBRARY_PATH=$(llvm-config --libdir)\n
Run Code Online (Sandbox Code Playgroud)\n\n

初始构建命令(不指定-lc++abi)有效,如上述 man 条目的第 5 条中所述。

\n