我正在尝试构建一个简单的(“hello world”)C++
使用 LLVM/Clang 3.7.0 的程序,根据工具链的源代码构建libc++
,使用命令行:
clang++ -std=c++14 -stdlib=libc++ -fno-exceptions hello.cpp
但是,我收到以下错误:
/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()'
[...]
The LD_LIBRARY_PATH
is not设置并将工具链的安装目录添加到我的工作中PATH
by:
export PATH=$PATH:/bulk/workbench/llvm/3.7.0/toolchain4/bin/
I'm on Ubuntu GNU/Linux 14.04
我还没有从任何存储库安装任何 LLVM 或 Clang 相关的软件包。
根据libc++ 文档 http://libcxx.llvm.org/docs/UsingLibcxx.html#using-libc-on-linux:
在 Linux 上,libc++ 通常只能与“-stdlib=libc++”一起使用。然而,某些 libc++ 安装需要用户自己手动链接 libc++abi。如果您在使用 libc++ 时遇到链接器错误,请尝试将“-lc++abi”添加到链接行。
按照建议进行操作即可成功构建。
所以,我的问题是这样的:
为什么我必须指定-lc++abi
明确依赖于构建命令行?
Doing
readelf -d $(llvm-config --libdir)/libc++.so
gives
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
[...]
嵌入式不应该吗RPATH
在 ELF 的动态部分可以考虑为ld
如其所述man page http://linux.die.net/man/1/ld在该部分下-rpath-link=dir
?
此外,当我设置LD_LIBRARY_PATH
with
LD_LIBRARY_PATH=$(llvm-config --libdir)
初始构建命令(不指定-lc++abi
) 有效,正如上述 man 条目的第 5 条中所述。