Use libc++ standard library implementation on GCC instead of libstdc++

Viewed 762

I'm trying to use the LLVM's standard library implementation (libc++) on GCC trunk (version 12.0.0 now) for the purpose of using the Formatter Library, which is implemented only in libc++ at this time (except MSVC). For this, I tried to compile with g++ in the following way:

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

Location for the dynamic libraries is specified in LD_LIBRARY_PATH and LIBRARY_PATH environment variables on Ubuntu 20.04 LTS Distro.

Code inside example.cpp is the following:

#include <format>
#include <string>
#include <iostream>

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

If I use clang-15 for this purpose, it compiles successfully and the command for compiling with clang is the following:

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

With GCC, the code is unable to compile because of the following errors (not all):

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'

I'm asking if that's something wrong with the GCC command that I use or what can be the reason of this from the 2 compilers sides (g++ and clang).

1 Answers

Solved! The problem is that the library path need to be explicitly added with -L GCC option, LD_LIBRARY_PATH or LIBRARY_PATH didn't work in this case, but not sure why.

Related