Skip to content

Instantly share code, notes, and snippets.

@Leedehai
Last active October 6, 2020 18:46
Show Gist options
  • Save Leedehai/4ed513a8efa835f09ebb1ed8b71eb43f to your computer and use it in GitHub Desktop.
Save Leedehai/4ed513a8efa835f09ebb1ed8b71eb43f to your computer and use it in GitHub Desktop.
So, your Clang wants to compile with libc++.

Clang with libc++

TL;DR: On macOS you are fine, if you didn't tweak its default compiler toolchain. If you did tweak it (e.g. use Clang downloaded from its official source or with a package manager), or if your OS is Linux, read below.

The C++ standard library has many well-maintained implementations, chief of them LLVM's libc++ and GNU's libstdc++. The standard library, including the headers and the binary itself, usually come with the downloaded compiler package: libc++ is with Clang and libstdc++ is with GCC.

If specified with -stdlib=libc++, the Clang compiler will compile with its headers and link with the library binary. Otherwise, the system default is used. The easiest way to verify is:

  1. Compile a simple program with clang++ your_program.cc -o your_program -stdlib=libc++ -v to check the headers are picked up from Clang's installation directory (where libc++'s headers are at), and
  2. Execute that program to ensure the program loader can discover the libc++ library binary (absense of libc++ won't trigger a link time error, because this is a dynamic library).

On macOS

The Apple Clang compiler coming with the developer toolchain or XCode has and uses libc++, and it is the system default. Therefore, you can rest assured it works.

However, if you use another version of Clang (usually downloaded from the official LLVM release website), you need to ensure

  • libc++ libray headers and binary is present (it should came with your Clang installation).
  • libc++ libray binary can be picked up by program loader by compiling and running a test program if not, append the library binary's directory to environment variable DYLD_LIBRARY_PATH) (note: otool -L [test_program] only shows paths of libraries need to load, but not whether they are present).
  • In Clang's compile and link commands, specify -stdlib=libc++[1]. Some libc++ installations may also require you to specify -lc++abi in the link command (TODO: how to verify this library is the one that came from the downloaded Clang, not the system-provided libc++)?

On Linux

The system default is libstdc++. Therefore, to ensure the headers and library binary used by Clang are from libc++, you need to manually make sure:

  • libc++ libray headers and binary is present (it should came with your Clang installation).
  • libc++ libray binary can be picked up by program loader by compiling and running a test program, or let ldd [test_program] show paths of libraries need to load and whether they are present (if not, append the library binary's directory to environment variable LD_LIBRARY_PATH).
  • In Clang's compile and link commands, specify -stdlib=libc++[1]. Some libc++ installations may also require you to specify -lc++abi in the link command.

[1] For link commands, if you use ld/lld/gold instead of letting Clang do it for you, be sure to specify the path to the libc++ binary.

Replacing libstdc++ with libc++ was motivated by the former's slow support of C++17 std::filesystem: even in the one that came with GCC 7.5, released in November 2019, it was still experimental.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment