1.8 Where <stdint.h> comes from: the headers GCC ships
GCC 15.2.0. To run the commands: an installed compiler, with g++ for the C++ search list.
You are building a sysroot for a cross target. You have copied in glibc's
headers, you have stdio.h, pthread.h and fcntl.h, and you check for
stdint.h and it is not there. You go looking for the glibc build step that
produces it, and there is none.
<stdint.h> is not the C library's header but the compiler's, and it lives
under GCC's own prefix, in $prefix/lib/gcc/<target>/<version>/include/. So do
<stddef.h>, <limits.h>, <stdarg.h> and about a dozen others. GCC searches
its own directories before the sysroot, and some of its headers then bring in
the C library's header of the same name with #include_next. The first command
prints the directory, and the second lists what GCC installed there:
$ gcc -print-file-name=include
$ ls "$(gcc -print-file-name=include)"
This chapter assumes Chapter 1.1, for the driver and
cc1, and Chapter 1.2, for the libsubdir, the
tooldir and the rule that the sysroot belongs to the C library and the prefix
belongs to GCC. That rule holds for headers without exception. The chapter also
uses %I from Chapter 1.3, -B and include_prefixes
from Chapter 1.5, and multiarch from
Chapter 1.6. It is the compile-time counterpart of
Chapter 1.7, which asks the same question of the link
line.
Freestanding headers such as <stdint.h>: why GCC provides them
The freestanding headers are a small core set that the C standard requires the compiler itself to provide. A conforming implementation must work in a freestanding environment, where by definition there is no C library to supply headers, so these headers must be available when nothing else is. That requirement is why they sit under GCC's prefix and not in the sysroot. The standard defines two kinds of execution environment:
| Environment | Means | __STDC_HOSTED__ |
|---|---|---|
| Hosted | a full OS with a complete C library: files, threads, <stdio.h> | 1 |
| Freestanding | bare metal, kernels, early boot: no OS, maybe no libc at all | 0 |
The GCC manual lists the freestanding set, standard by standard, under
Language Standards Supported by GCC. GCC keeps the sources
under gcc/ginclude/, and a makefile variable, USER_H, lists the
ones that get installed:
USER_H = $(srcdir)/ginclude/float.h \
$(srcdir)/ginclude/iso646.h \
$(srcdir)/ginclude/stdarg.h \
$(srcdir)/ginclude/stdbool.h \
$(srcdir)/ginclude/stddef.h \
$(srcdir)/ginclude/varargs.h \
$(srcdir)/ginclude/stdfix.h \
$(srcdir)/ginclude/stdnoreturn.h \
$(srcdir)/ginclude/stdalign.h \
$(srcdir)/ginclude/stdatomic.h \
$(srcdir)/ginclude/stdckdint.h \
$(EXTRA_HEADERS)
Listing 1.8-1: the headers GCC installs (gcc/Makefile.in:473-484).
The last entry, $(EXTRA_HEADERS), is where a target adds headers of its own,
which is how <arm_neon.h> and <immintrin.h> get there. <stdint.h> and
<limits.h> are not in the list, because the build installs them by separate
rules, which the #include_next section reads.
Before GCC 14.
<stdckdint.h>is not there. C23's checked integer arithmetic header arrived in GCC 14:gcc/ginclude/stdckdint.hdoes not exist atreleases/gcc-13.3.0and does atreleases/gcc-14.3.0.
Each of these headers encodes something only the compiler knows, such as type widths, alignment rules, calling conventions or instruction sets, so a C library cannot get them right for an arbitrary compiler. The table gives what each header provides and why it must be the compiler's:
| Header | Provides | Why it must be the compiler's |
|---|---|---|
<stddef.h> | size_t, ptrdiff_t, NULL, offsetof, wchar_t | These are compiler properties, and offsetof is __builtin_offsetof |
<stdarg.h> | va_list, va_start, va_arg, va_end | Expands to __builtin_va_*: the varargs ABI is generated code, not a library |
<stdint.h> | int32_t, uint64_t, INTPTR_MAX, … | Must match the target data model: ILP32, LP64, LLP64 |
<limits.h> | INT_MAX, CHAR_BIT, LONG_MIN | Derived from the target's integer widths |
<float.h> | FLT_MANT_DIG, DBL_MAX, … | Derived from the target's floating-point formats |
<stdbool.h> | bool, true, false | Maps to the built-in _Bool |
<stdalign.h> | alignas, alignof | Maps to _Alignas and _Alignof |
<stdatomic.h> | atomic_int, atomic_load, … | Atomics are codegen plus libatomic, not libc |
<iso646.h> | and, or, not, … | Pure macros, with nothing for a libc to contribute |
| arch intrinsics | <arm_neon.h>, <immintrin.h>, … | One-to-one mappings onto instructions the back end knows |
C23 made bool, alignas, alignof and noreturn keywords, so
<stdbool.h>, <stdalign.h> and <stdnoreturn.h> are now largely vestigial,
but they still exist and still come from GCC. <stdbit.h> is C23 too, but it is
not one of GCC's headers. In GCC 15.2.0 it exists only as a libstdc++
C-compatibility header
(libstdc++-v3/include/c_compatibility/stdbit.h), not in
gcc/ginclude/. Run ls on your own -print-file-name=include rather than
trusting any list, including Listing 1.8-1.
include-fixed/: system headers patched by fixincludes at build time
include-fixed/ holds patched copies of the system's headers, and include/
holds GCC's own. Both sit in the libsubdir, and both come before the sysroot in
the search order. include-fixed/ is a snapshot taken when GCC was built, so a
header the compile takes from it can be stale against a sysroot updated later.
fixincludes is a build-time pass that copies the system headers it finds
and mechanically repairs constructs GCC dislikes, such as ancient K&R
declarations and macros that break under a modern preprocessor. Its output is
include-fixed/. The machinery lives in fixincludes/, and its
README describes the workflow, which is a rule database compiled by AutoGen:
If you are having some problem with a system header that is either broken by the manufacturer, or is broken by the fixinclude process, then you will need to alter or add information to the include fix definitions file,
inclhack.def.
If your sysroot's headers were updated after GCC was built, you compile against
a patched version of a header that no longer exists. On a modern glibc or musl
target include-fixed/ is usually near-empty. The machinery is mostly
historical, but on older or unusual targets it can matter a great deal.
The compiled-in list of header directories has separate entries for
include-fixed/, and they have their own multilib behaviour:
#ifdef FIXED_INCLUDE_DIR
/* This is the dir for fixincludes. */
#ifndef SYSROOT_HEADERS_SUFFIX_SPEC
{ FIXED_INCLUDE_DIR, "GCC", 0, 0, 0, 2 },
#endif
{ FIXED_INCLUDE_DIR, "GCC", 0, 0, 0,
/* A multilib suffix needs adding if different multilibs use
different headers. */
Listing 1.8-2: the include-fixed entries
(gcc/cppdefault.cc:75-83).
The trailing 2 is the multilib field, and it means "append the multiarch
path". The three-way split of Chapter 1.6, between
GCC's multilib directory, the OS one and the multiarch one, therefore reaches
into header search too.
cpp_include_defaults: the header search order, GCC's directories first
cpp_include_defaults is the compiled-in array that sets the header search
order, and it lists GCC's own directories before the C library's.
#include <limits.h> therefore finds GCC's copy first, and that copy decides
whether to bring in the C library's as well. gcc -v -E prints the list, and
the cpp manual documents this use of -v under Search Path:
$ gcc -v -E - < /dev/null
For an arm-none-eabi toolchain installed in /opt/gcc-arm, the <...> part of
the list has four entries. GCC's own include/ and include-fixed/ come first,
the tooldir's include/ next, and the C library's directory in the sysroot last:
#include <...> search starts here:
/opt/gcc-arm/lib/gcc/arm-none-eabi/15.2.0/include ← GCC's own
/opt/gcc-arm/lib/gcc/arm-none-eabi/15.2.0/include-fixed ← fixincludes output
/opt/gcc-arm/arm-none-eabi/include ← tooldir
<sysroot>/usr/include ← the C library's
End of search list.
The array is defined at gcc/cppdefault.cc:38, and the search
order is its declaration order. The entries run from the C++ header directories
at the top down to the native system directory, whose comment says where it
ranks:
#ifdef NATIVE_SYSTEM_HEADER_DIR
/* /usr/include comes dead last. */
{ NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 2 },
{ NATIVE_SYSTEM_HEADER_DIR, NATIVE_SYSTEM_HEADER_COMPONENT, 0, 0, 1, 0 },
#endif
Listing 1.8-3: the last entries in the list
(gcc/cppdefault.cc:98-102).
The compiler's makefile #defines the macros that fill in the main entries,
and the table gives the directory that each one names:
| Macro | Directory |
|---|---|
GCC_INCLUDE_DIR | $libsubdir/include, GCC's own |
FIXED_INCLUDE_DIR | $libsubdir/include-fixed |
TOOL_INCLUDE_DIR | $tooldir/include (gcc/Makefile.in:2613) |
NATIVE_SYSTEM_HEADER_DIR | /usr/include, sysrooted |
GPLUSPLUS_INCLUDE_DIR | the C++ headers (Chapter 1.12) |
TOOL_INCLUDE_DIR is where a bare-metal C library such as newlib installs its
headers (Chapter 3.4). A pure-sysroot
toolchain that never asked for it still searches it. The pre-sysroot
--with-headers option of Chapter 1.2 copies the
headers into a different directory, $tooldir/sys-include.
add_sysroot: which header directories follow --sysroot
add_sysroot is a field in each entry of cpp_include_defaults that says
whether --sysroot applies to that directory. The decision is therefore made per
directory, and no flag can change it after the fact. GCC's own header
directories are not sysrooted but relocated with the compiler, which is why
--sysroot cannot move <stdint.h>. The field's declaration reads:
const char add_sysroot; /* FNAME should be prefixed by
cpp_SYSROOT. */
Listing 1.8-4: the field (gcc/cppdefault.h:48-49).
The code that builds the search path in incpath.cc branches on the field for
each directory. The else if is the branch to read:
/* Should this directory start with the sysroot? */
if (sysroot && p->add_sysroot)
{
...
str = concat (sysroot_no_trailing_dir_separator, p->fname, NULL);
...
}
else if (!p->add_sysroot && relocated
&& !filename_ncmp (p->fname, cpp_PREFIX, cpp_PREFIX_len))
{
Listing 1.8-5: sysroot or relocate, per directory
(gcc/incpath.cc:181-193).
GCC's own header directories take the else if branch. If such a path starts
with the configure-time prefix and the compiler has been relocated, the run-time
prefix is substituted for it. The branch is the rule that these headers are
GCC's and not the C library's, stated in code. It is also the consumer of the
set_std_prefix call that Chapter 1.9 shows in
Listing 1.9-2.
The split parallels add_prefix against add_sysrooted_prefix for libraries in
Chapter 1.5, but the implementation is separate.
Header search and library search are two mechanisms that the same prefix
relocates.
#include_next: how GCC's headers wrap the C library's
#include_next is the GNU extension that lets one of GCC's headers wrap the C
library's header of the same name. GCC's copy adds what the compiler knows, and
the C library's copy still contributes its own definitions. <stdint.h> and
<limits.h> work this way when the system has its own copy, and -H shows the
chain on any compiler. The cpp manual documents the directive under
Wrapper Headers.
#include <foo.h> searches the path from the beginning, and
#include_next <foo.h> searches only the directories after the one where the
current file was found. A header can therefore include the next file of the same
name further down the search path, one layer below itself, without knowing where
that layer is and without the risk of including itself.
<stdint.h>: wrapped when hosted, self-contained when freestanding
<stdint.h> is the clearest wrapper, because its shipped source holds the whole
hosted and freestanding split. A hosted compile defers to the system's copy. A
freestanding compile may have no system copy at all, so it falls back to
stdint-gcc.h, a self-contained version that GCC ships alongside:
#ifndef _GCC_WRAP_STDINT_H
#if __STDC_HOSTED__
...
# include_next <stdint.h>
...
#else
# include "stdint-gcc.h"
#endif
#define _GCC_WRAP_STDINT_H
#endif
Listing 1.8-6: stdint-wrap.h, abridged, without the lines that define
__STDC_LIMIT_MACROS for C++11 and silence a -Wpedantic warning about
include_next (gcc/ginclude/stdint-wrap.h).
The #if __STDC_HOSTED__ test meets the freestanding requirement in eight
lines. Whether this wrapper becomes include/stdint.h at all is a separate,
build-time decision. USE_GCC_STDINT comes from configure, and the install rule
copies the wrapper when it is wrap and the self-contained version when it is
provide:
if [ $(USE_GCC_STDINT) = wrap ]; then \
cp $(srcdir)/ginclude/stdint-wrap.h include/stdint.h; \
...
elif [ $(USE_GCC_STDINT) = provide ]; then \
cp $(T_STDINT_GCC_H) include/stdint.h; \
Listing 1.8-7: wrap or provide (gcc/Makefile.in:3514-3524).
The value of USE_GCC_STDINT comes from configure (:816).
<limits.h> is assembled, not shipped
<limits.h> is a wrapper too, but there is no single gcc/limits.h in the tree
to read, because the build concatenates the installed file from three
fragments. A filesystem test at build time decides which fragments go in:
if $(LIMITS_H_TEST) ; then \
cat $(srcdir)/limitx.h $(T_GLIMITS_H) $(srcdir)/limity.h > tmp-xlimits.h; \
else \
cat $(T_GLIMITS_H) > tmp-xlimits.h; \
fi; \
Listing 1.8-8: building limits.h (gcc/Makefile.in:3528-3532).
LIMITS_H_TEST is one line, and it tests for the system's own limits.h:
LIMITS_H_TEST = [ -f $(BUILD_SYSTEM_HEADER_DIR)/limits.h ]
Listing 1.8-9: LIMITS_H_TEST (gcc/Makefile.in:591).
If the system has its own limits.h, the installed file is the wrapper form:
the prologue limitx.h, GCC's own limits, and the epilogue limity.h. If it
does not, the file is GCC's limits alone, with no #include_next at all. The
fragments say so themselves:
/* This administrivia gets added to the beginning of limits.h
if the system has its own version of limits.h. */
Listing 1.8-10: the prologue's comment (gcc/limitx.h:24-25). The
matching comment in gcc/limity.h:1-2 says "the end of limits.h".
limitx.h does not use #include_next itself, so the prologue reaches the
system's limits.h through one more file. It includes syslimits.h from its
own directory:
/* Use "..." so that we find syslimits.h only in this same directory. */
#include "syslimits.h"
Listing 1.8-11: the hop to syslimits.h (gcc/limitx.h:33-34).
syslimits.h is a copy of gsyslimits.h
(gcc/Makefile.in:3540), and its body is only these lines:
#define _GCC_NEXT_LIMITS_H /* tell gcc's limits.h to recurse */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" // include_next
#include_next <limits.h>
#pragma GCC diagnostic pop
#undef _GCC_NEXT_LIMITS_H
Listing 1.8-12: gsyslimits.h (gcc/gsyslimits.h:6-11).
The extra file exists because include-fixed/ may contain a patched
syslimits.h instead, which is one of fixincludes' jobs. The hop gives the build
a place to intervene without touching limits.h itself.
Checking #include_next with -H
-H prints the include tree, one dot of indentation per nesting level, so each
#include_next hop shows up one level deeper. Which headers are wrappers varies
by target, so run -H on your own compiler rather than trusting a table. The
cpp manual documents -H under Invocation.
$ echo '#include <limits.h>' | gcc -H -E -x c - > /dev/null
The tree shows GCC's limits.h at depth one, syslimits.h below it and the
sysroot's limits.h below that, which is the hop of Listing 1.8-11 made
visible. Roughly, <stddef.h>, <stdarg.h>, <stdbool.h>, <iso646.h> and
the arch intrinsics are standalone, with no #include_next.
Checking header search with -print-file-name=include and -v -E
-print-file-name=include and gcc -v -E answer the two questions behind a
missing or wrong header: where GCC's own headers are, and which directories the
compile searches, in order. With ls, -H and a grep, they make five
commands to run on any toolchain:
$ gcc -print-file-name=include # GCC's own header dir
$ ls "$(gcc -print-file-name=include)" # what it actually ships
$ gcc -v -E - < /dev/null # the full search list
$ echo '#include <limits.h>' | gcc -H -E -x c - >/dev/null # the include_next chain
$ echo '#include <stdint.h>' | gcc -E -x c - | grep -m1 stdint
The last command shows which file a single #include resolved to. Of the
others, gcc -v -E - < /dev/null is the only way to see the whole list. -
reads an empty program from standard input, -E stops after preprocessing, and
-v lets you see cc1's standard error. The list comes from cc1, not from
the driver, and cc1 prints it only when it is verbose:
/* If verbose, print the list of dirs to search. */
if (verbose)
{
fprintf (stderr, _("#include \"...\" search starts here:\n"));
...
fprintf (stderr, _("End of search list.\n"));
Listing 1.8-13: the verbose printout
(gcc/incpath.cc:391-405).
Before GCC 15. The printout has one section fewer. GCC 15 added a fifth include chain for C23's
#embed, somerge_include_chainsnow sysroot-expands the quote, bracket, system, after and embed chains (gcc/incpath.cc:359-363), and the verbose output gained an#embed <...> search starts here:block (:408).INC_EMBEDis absent at every release tag through 14.3.0 and present at 15.2.0.
For C++ you must name the language with -x c++, because standard input has no
extension to infer a language from. The list then starts with the
include/c++/... directories:
$ g++ -v -E -x c++ /dev/null
If the include/c++/... entries begin with your sysroot path, the toolchain was
configured for sysroot-relative C++ headers. If they do not, they are
prefix-relative, and --sysroot never moves them.
Chapter 1.12 explains the difference.
Three things to carry away
<stdint.h> is the compiler's, not the C library's. It lives under
lib/gcc/<target>/<version>/include/, and gcc -print-file-name=include prints
that directory.
GCC's header directories come before the system's. In a hosted build GCC's copy defers to the system's. In a freestanding build GCC's copy is the whole answer.
include-fixed/ is a snapshot taken when GCC was built. It can be stale
against a sysroot you updated later.
Things that surprise people
Do not look in the sysroot for stdint.h. It is not there and it is not
supposed to be. gcc -print-file-name=include is where to look.
Copying a sysroot does not copy GCC's headers. A sysroot is complete for libc but useless on its own, because the compiler's own headers travel with the compiler.
GCC's headers are version-locked. The path contains <version>
(Listing 1.2-2), so two GCC releases have separate copies. Mixing GCC 12's
headers with GCC 15's cc1 is a broken toolchain, not only an unwise one.
-nostdinc removes GCC's headers too, not only the system directories
(gcc/c-family/c.opt:2523). On bare metal you almost always
want them back:
$ gcc -nostdinc -isystem "$(gcc -print-file-name=include)" …
-ffreestanding changes nothing about header search. Its entry in c.opt
has one line of help text:
ffreestanding
C ObjC C++ ObjC++
Do not assume that standard C libraries and \"main\" exist.
Listing 1.8-14: the ffreestanding entry
(gcc/c-family/c.opt:1986-1988).
It changes what the compiler may assume. It stops printf("x\n") from becoming
puts("x"), relaxes the requirements on main and sets __STDC_HOSTED__ to
0, but it moves no directory. Chapter 1.10
puts it side by side with -nostdlib and -nostdinc, the two options it is most
often confused with. On a hosted target, the first command below shows
__STDC_HOSTED__ defined to 1, and the second shows it defined to 0:
$ gcc -dM -E - < /dev/null | grep __STDC_HOSTED__
$ gcc -ffreestanding -dM -E - < /dev/null | grep __STDC_HOSTED__
-B moves GCC's header directories. A -B<dir> adds
<dir>/<target>/<version>/include and .../include-fixed to the header search,
because -B feeds include_prefixes (Listing 1.5-2) and %I expands that list:
info.option = "-isystem";
info.append = "include";
...
for_each_path (&include_prefixes, false, info.append_len,
spec_path, &info);
Listing 1.8-15: %I emitting GCC's header directories
(gcc/gcc.cc:6604-6612).
So -B can change which <stdint.h> you compile against, but not which
<vector> you get, because only GCC's own two directories are in
include_prefixes.
Documentation coverage
The GCC manual documents -nostdinc and -I, -isystem and -idirafter under
Directory Options (gcc/doc/invoke.texi:19484). The
preprocessor manual documents them too, along with #include_next and -H.
-ffreestanding is under C Dialect Options
(gcc/doc/invoke.texi:2665).
What is not documented:
- That
<limits.h>is assembled from three fragments at build time, and that which form you get depends on a filesystem test. Thesyslimits.hhop in particular exists only in the source. - That the sysroot decision is per include-directory, carried in a struct field, and therefore not something a flag can change after the fact.
- That GCC's own header directories are relocated rather than sysrooted. The
else ifin Listing 1.8-5 is the precise statement of why--sysrootcannot move them.
Next: Chapter 1.9, How GCC_EXEC_PREFIX and LIBRARY_PATH move the search paths,
the environment variables that move both search lists without appearing on any
command line.