在GCC上使用libc++标准库实现,而不是libstdc++



我正试图在GCC主干(现在是12.0.0版本(上使用LLVM的标准库实现(libc++(来使用格式化程序库,该库目前仅在libc++中实现(MSVC除外(。为此,我尝试以以下方式使用g++进行编译:

g++ -std=c++23 -nostdinc++ -nodefaultlibs -I<path-to-libc++>/include/c++/v1 -I<path-to-libc++>/include/x86_64-unknown-linux-gnu/c++/v1 example.cpp -lc -lc++ -lc++abi -lunwind

动态库的位置在Ubuntu 20.04 LTS Distro上的LD_LIBRARY_PATH和LIBRARY_PATH环境变量中指定。

example.cpp中的代码如下:

#include <format>
#include <string>
#include <iostream>
int main() {
std::string str = std::format("{}", "Hello");
std::cout << str << 'n';
}

如果我将clang-15用于此目的,它将成功编译,使用clang编译的命令如下:

clang++ -std=c++2b -stdlib=libc++ example.cpp

使用GCC时,由于以下错误(并非全部(,代码无法编译:

a.cpp:(.text+0x837): undefined reference to `vtable for std::__1::format_error'
/usr/bin/ld: a.cpp:(.text+0x847): undefined reference to `std::__1::format_error::~format_error()'
/usr/bin/ld: a.cpp:(.text+0x84c): undefined reference to `typeinfo for std::__1::format_error'

我想问的是,我使用的GCC命令是否有问题,或者是什么原因导致了这两个编译器(g++和clang(的问题。

已解决!问题是库路径需要显式添加-LGCC选项,LD_LIBRARY_PATHLIBRARY_PATH在这种情况下不起作用,但不确定原因。

最新更新