clang++: Usage, Options & Common CI Errors
clang++ compiles C++ with LLVM, optionally against libc++ instead of libstdc++.
clang++ is the C++ driver for clang. On Linux it defaults to libstdc++ but can target LLVM’s libc++ with -stdlib=libc++ - mixing the two across objects is the classic CI link failure.
What it does
clang++ compiles and links C++ using LLVM. It links a C++ standard library automatically (libstdc++ on Linux by default, libc++ with -stdlib=libc++) and supports the same sanitizers and standards flags as clang.
Common usage
clang++ -std=c++20 -Wall -O2 main.cpp -o app
clang++ -stdlib=libc++ -std=c++20 main.cpp -o app
clang++ -c util.cpp -o util.o
clang++ -fsanitize=address -g main.cpp -o app
clang++ main.cpp -o app -lpthreadOptions
| Flag | What it does |
|---|---|
| -std=c++17|c++20|c++23 | C++ language standard |
| -stdlib=libc++|libstdc++ | Choose the C++ standard library |
| -c / -o <file> | Compile only / name output |
| -fsanitize=address|undefined | Enable a sanitizer |
| -l<name> -L<dir> -I<dir> | Libraries and search paths |
Common errors in CI
Linking objects built with -stdlib=libc++ against ones built with libstdc++ yields a flood of "undefined reference" errors because the std::__1 vs std:: symbol namespaces differ - build every object and dependency with the same -stdlib. "fatal error: ‘cstdio’ file not found" with -stdlib=libc++ means the libc++ dev headers are not installed (libc++-dev). For the GCC standard library the -std flag must match what dependencies expect.