Clang fails to compile libstdc++ headers due to incorrect __GLIBCXX_TYPE_INT_N_0 handling
Clang fails to compile libstdc++ headers due to incorrect __GLIBCXX_TYPE_INT_N_0 handling
Motivation
Clang is commonly used together with GCC's libstdc++ on Linux.
This configuration is expected to work:
clang++ + libstdc++ (from GCC)
However, with recent GCC libstdc++ headers (GCC 16) and Clang 21, certain compilation modes cause duplicated definitions inside libstdc++ headers.
The failure happens in standard headers such as:
<type_traits><bits/cpp_type_traits.h><bits/stl_algobase.h><bits/std_abs.h><bits/functional_hash.h>
The user should not need to modify compiler predefined macros manually to use a supported Clang + libstdc++ configuration.
Environment
OS: Ubuntu 26.04
clang++ --version:
Ubuntu clang version 21.1.8 LLVM version 21.1.8
gcc:
gcc version 16.x
libstdc++:
/usr/lib/gcc/x86_64-linux-gnu/16/include/c++
Reproduction
test.cpp
#include <type_traits>
#include <iostream>
int main()
{
static_assert(std::is_integral<__int128>::value);
std::cout << "ok\n";
}
Compile:
clang++ test.cpp \
-std=gnu++11 \
-D__STRICT_ANSI__
Result:
error: redefinition of '__is_integral_helper<__int128>'
note: previous definition is here
struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_0>
...
error: redefinition of '__make_unsigned<__int128>'
...
error: redefinition of 'hash<__int128>'
Expected behavior
The program should compile successfully.
Clang should be able to consume GCC's libstdc++ headers without internal header conflicts.
Actual behavior
__GLIBCXX_TYPE_INT_N_0 expands to __int128, causing libstdc++ to generate duplicated specializations.
For example:
__GLIBCXX_TYPE_INT_N_0
and:
__int128
become identical types.
This produces duplicate definitions:
template<>
struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_0>;
template<>
struct __is_integral_helper<__int128>;
which are the same specialization after macro expansion.
Additional information
The issue is reproducible only with Clang + GCC libstdc++.
GCC:
g++ test.cpp -std=gnu++11 -D__STRICT_ANSI__
works.
Clang with libc++:
clang++ -stdlib=libc++
works.
Clang with libstdc++:
clang++ -std=gnu++11
works in normal mode.
The failure requires the interaction between Clang's predefined macros and libstdc++ feature detection.
This is expected behavior; the issue is defining __STRICT_ANSI__ along with a GNU language mode. GCC has the same failures: https://godbolt.org/z/Evdjdb9TE
If you do not define __STRICT_ANSI__, or you use c++11 instead of gnu++11, both Clang and GCC accept: https://godbolt.org/z/Wrfc5xheP