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 andlibstdc++
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:
- 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 (wherelibc++
's headers are at), and - Execute that program to ensure the program loader can discover the
libc++
library binary (absense oflibc++
won't trigger a link time error, because this is a dynamic library).
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 variableDYLD_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++)?
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 letldd [test_program]
show paths of libraries need to load and whether they are present (if not, append the library binary's directory to environment variableLD_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 thelibc++
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.
■