1.12 Which libstdc++, and which <vector>

GCC 15.2.0. To run the commands: a C++ cross compiler such as arm-linux-gnueabihf-g++, and a target root filesystem for --sysroot.

You have a cross toolchain and a root filesystem for the target. You pass --sysroot=/arm-rootfs, and the rootfs contains /usr/include/c++/15 and /usr/lib/libstdc++.so.6. Yet g++ -v -E -x c++ /dev/null shows a <vector> from a directory under your compiler's install prefix. You try -B to fix it, and nothing changes.

Neither half of libstdc++ is in the sysroot, by design. The headers and the library both live relative to where the compiler is installed, and --sysroot is about the C library. Exactly one configure-time route makes the headers sysroot-relative, and you have to ask for it.

Chapter 1.11 put -lstdc++ on the link line. This chapter answers two further questions: which file that -l resolves to, and which <vector> you compiled against. Different mechanisms answer them, although the same prefix moves both.

This chapter assumes Chapter 1.2 for the three machine names and the tooldir, and Chapter 1.5 for the driver's prefix lists, add_prefix against add_sysrooted_prefix, and Listings 1.5-8 and 1.5-9. It uses the multilib expansion of Chapter 1.6, and cpp_include_defaults, its add_sysroot field and %I from Chapter 1.8. It also uses GCC_EXEC_PREFIX and LIBRARY_PATH from Chapter 1.9, -nostdinc++ from Chapter 1.10 and -stdlib= from Chapter 1.11.

-lstdc++, <vector> and libstdc++.so: three separate decisions

Three questions about the C++ runtime look like one, and three separate mechanisms answer them: whether -lstdc++ goes on the link line, where #include <vector> comes from, and which libstdc++.so the -l finds. --sysroot affects each mechanism differently, as the last column shows, so a fix has to start from the right question:

QuestionAnswered bySysroot-aware?
Should -lstdc++ be on the link line?the g++ driver (Chapter 1.11)no, pure option rewriting
Where does #include <vector> come from?GCC's compiled-in include tableonly if configured for it
Which libstdc++.so does that -l find?the driver's prefix list → -L flags → ldyes, but the sysroot usually loses

The third row follows from a rule of Chapter 1.5: the driver never opens libstdc++.so. It emits one -l and a list of -L options and lets ld resolve them, so the question of which library is the question of which directory comes first.

Where a cross install puts libstdc++

A cross build, one whose host and target differ, installs libstdc++ into the tooldir, the target's own subtree of the install, and not into the sysroot or $prefix/lib. The driver must later search the same places, so these two install paths are the reference for every search question about libstdc++:

ArtefactInstall location
Headers$prefix/$target_alias/include/c++/$version
libstdc++.{a,so}$exec_prefix/$target_alias/lib[/$multi_os_dir]

The install manual gives the library directory under Configuration, as the default of --with-toolexeclibdir: ${gcc_tooldir}/lib. For the headers it says only that the default "differs between cross and native configurations".

Both paths come from one autoconf macro, GLIBCXX_EXPORT_INSTALL_INFO (libstdc++-v3/acinclude.m4:729). The file changes between point releases and in vendor forks, so read it at the releases/gcc-15.2.0 tag when you follow these line numbers. The macro computes the header directory first. In the default case, where $version_specific_libs and $gxx_include_dir are both no, it puts include/c++/${gcc_version} under ${prefix}/${target_alias} when $with_cross_host is set to something other than no, and under ${prefix} otherwise:

  # Default case for install directory for include files.
  if test $version_specific_libs = no && test $gxx_include_dir = no; then
    gxx_include_dir='include/c++/${gcc_version}'
    if test -n "$with_cross_host" &&
       test x"$with_cross_host" != x"no"; then
      gxx_include_dir='${prefix}/${target_alias}/'"$gxx_include_dir"
    else
      gxx_include_dir='${prefix}/'"$gxx_include_dir"
    fi
  fi

Listing 1.12-1: the header install directory (libstdc++-v3/acinclude.m4:760-769).

The library directory follows. Its comment states the policy, and the formula puts the tooldir under ${exec_prefix}:

  # Calculate glibcxx_toolexecdir, glibcxx_toolexeclibdir
  # Install a library built with a cross compiler in tooldir, not libdir.
  if test x"$glibcxx_toolexecdir" = x"no"; then
    if test -n "$with_cross_host" &&
       test x"$with_cross_host" != x"no"; then
      glibcxx_toolexecdir='${exec_prefix}/${host_alias}'
      case ${with_toolexeclibdir} in
	no)
	  glibcxx_toolexeclibdir='${toolexecdir}/lib'
	  ;;

Listing 1.12-2: the library install directory (libstdc++-v3/acinclude.m4:784-793).

$exec_prefix and ${host_alias} in the libstdc++ install paths

The libstdc++ install paths use two variables that are easy to misread: $exec_prefix, which can differ from $prefix, and ${host_alias}, which in a target library names GCC's target. Both decide where the files land, and a misreading of either shows only after a build has gone wrong.

$prefix and $exec_prefix are different variables that default to the same value, which is why the difference goes unnoticed. Listing 1.12-1 builds the header path from ${prefix}, and Listing 1.12-2 builds the library path from ${exec_prefix}. If you pass --exec-prefix, your C++ headers and your C++ library land in different trees from one configure run, with no warning. The install manual documents both options under Configuration, where --exec-prefix is the root for architecture-dependent files.

${host_alias} in a target library means GCC's target. Target libraries are configured with --host=<target triple>, because from libstdc++'s point of view the machine it will run on is GCC's target. ${exec_prefix}/${host_alias} in Listing 1.12-2 is therefore $exec_prefix/<target>, the tooldir, and libgcc uses the same convention with real_host_noncanonical. Whenever a path formula inside a target library says "host", read "GCC's target": the three machines of Chapter 1.2, seen from the other end.

gcc_gxx_include_dir: the driver's copy of the C++ header path

The driver must search where the install rules put libstdc++, so gcc/configure.ac computes the header path a second time, as gcc_gxx_include_dir. For the library it computes no path, and adds the tooldir's lib/ directory to its search list instead. The comment at the top of Listing 1.12-3 states the rule that the two copies follow:

# This logic must match libstdc++-v3/acinclude.m4:GLIBCXX_EXPORT_INSTALL_INFO.
if test x${gcc_gxx_include_dir} = x; then
  if test x${enable_version_specific_runtime_libs} = xyes; then
    gcc_gxx_include_dir='${libsubdir}/include/c++'
  else
    libstdcxx_incdir='include/c++/$(version)'
    if test x$host != x$target; then
       libstdcxx_incdir="$target_alias/$libstdcxx_incdir"
    fi
    gcc_gxx_include_dir="\$(libsubdir)/\$(libsubdir_to_prefix)$libstdcxx_incdir"
  fi

Listing 1.12-3: the driver's half (gcc/configure.ac:213-223).

The duplication spans three files, not two. libstdc++'s copy of the comment names a third, config/gxx-include-dir.m4, that must be kept consistent too (acinclude.m4:727-728). A change to one of the three needs the same change in the other two, or the headers install where the driver will not look.

The library half is one call, on startfile_prefixes, the driver's library search list:

  add_prefix (&startfile_prefixes,
	      concat (tooldir_prefix, "lib", dir_separator_str, NULL),
	      "BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);

Listing 1.12-4: the tooldir on the library search list (gcc/gcc.cc:5537-5539).

Listing 1.12-4 calls add_prefix, not add_sysrooted_prefix, and that one call is why --sysroot does not move your C++ library. Listing 1.5-9 in Chapter 1.5 registers /lib/ and /usr/lib/ through add_sysrooted_prefix, which prepends target_system_root to each prefix (gcc/gcc.cc:3177-3205). add_prefix prepends nothing.

--sysroot and the C++ headers: the per-entry add_sysroot flag

The C++ headers follow --sysroot only if configure set the add_sysroot flag on their entry in the compiled-in include table, cpp_include_defaults. Configure sets the flag only for a --with-gxx-include-dir path inside the --with-sysroot path, and that is the one configure-time route to sysroot-relative headers. Each entry carries its own flag, so the setting is per directory, not global:

  const char add_sysroot;	/* FNAME should be prefixed by
				   cpp_SYSROOT.  */

Listing 1.12-5: the per-entry sysroot flag (gcc/cppdefault.h:48-49).

For the C++ directories, the field's value is a configure-time macro, GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT:

#ifdef GPLUSPLUS_INCLUDE_DIR
    /* Pick up GNU C++ generic include files.  */
    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1,
      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
#endif

Listing 1.12-6: the C++ header entry (gcc/cppdefault.cc:43-47).

Because the decision is in the table, per entry, no command-line flag can change it after the fact. --sysroot supplies the string, and whether a given directory uses it was decided when GCC was configured. The macro's value comes from gcc_gxx_include_dir_add_sysroot, which starts at zero (gcc/configure.ac:211) and is set to 1 in exactly one place:

elif test "${with_sysroot+set}" = set; then
  gcc_gxx_without_sysroot=`expr "${gcc_gxx_include_dir}" : "${with_sysroot}"'\(.*\)'`
  if test "${gcc_gxx_without_sysroot}"; then
    gcc_gxx_include_dir="${gcc_gxx_without_sysroot}"
    gcc_gxx_include_dir_add_sysroot=1
  fi
fi

Listing 1.12-7: the one place that sets the flag (gcc/configure.ac:224-230).

The elif is the else-branch of if test x${gcc_gxx_include_dir} = x in Listing 1.12-3, so it runs only when you passed --with-gxx-include-dir. Without that option the default branch runs, the path is prefix-relative, and the flag stays 0. With it, configure factors the sysroot out only if your path starts with the text of --with-sysroot's value, because the expr is a string match, not a path comparison. The two conditions give four configurations:

Configurationinclude/c++/15 resolves toFollows --sysroot?
plain cross, no --with-gxx-include-dir$prefix/$target/include/c++/$ver, relocatableno
--with-sysroot=S onlysame as aboveno
--with-sysroot=S --with-gxx-include-dir=S/usr/include/c++/15<sysroot>/usr/include/c++/15yes
--with-gxx-include-dir=/opt/foo, no overlap with the sysroot/opt/foo, absoluteno

Only the third row gives the sysroot-native layout, and it needs both options, with one path textually inside the other. The default gives relocatability instead, which is usually worth more.

The default include/c++: relocated with the installed prefix

The default include/c++ directories are relocated with the compiler rather than sysrooted, which is why --sysroot cannot move them. When the compiler has been relocated, add_standard_paths rewrites a standard directory that is not sysrooted and starts with the configure-time prefix, so that it starts at the run-time prefix instead. The comment in Listing 1.12-8 names that prefix:

	  else if (!p->add_sysroot && relocated
		   && !filename_ncmp (p->fname, cpp_PREFIX, cpp_PREFIX_len))
	    {
 	      static const char *relocated_prefix;
	      char *ostr;
	      /* If this path starts with the configure-time prefix,
		 but the compiler has been relocated, replace it
		 with the run-time prefix.  The run-time exec prefix
		 is GCC_EXEC_PREFIX.  Compute the path from there back
		 to the toplevel prefix.  */

Listing 1.12-8: the relocation branch (gcc/incpath.cc:192-201).

A default cross build takes this branch, so if you move the installed toolchain, the C++ headers follow it. The path is recomputed from GCC_EXEC_PREFIX (Chapter 1.9) rather than stored as an absolute string. The sysroot branch and this one exclude each other by construction, so GCC's own header directories are relative to a prefix discovered at run time, never to the sysroot.

-B cannot move the C++ headers either, which is why the -B of the opening example changed nothing. -B feeds include_prefixes, and %I turns that list into -isystem <B>/<target>/<version>/include and .../include-fixed, GCC's own header directories from Chapter 1.8. The C++ directories are not in that set, so -B changes which <stdint.h> you get but not which <vector>. The GCC manual documents the translation into -isystem in its -B item, under Directory Options.

-nostdinc++ and -isystem =: sysroot-relative C++ headers

-nostdinc++ and an =-prefixed -isystem replace the C++ headers of one compilation with sysroot-relative ones, whatever GCC's configuration. You can therefore test a sysroot-resident header set against a compiler that was not configured for one. -nostdinc++ drops the compiled-in C++ directories, and a leading = or $SYSROOT on any -I, -isystem or -idirafter path expands to the sysroot. The GCC manual documents the = rule and -nostdinc++ under Directory Options.

add_sysroot_to_chain applies the = and $SYSROOT rule to the include chains:

      if (p->user_supplied_p)
	{
	  if (p->name[0] == '=')
	    p->name = concat (sysroot, p->name + 1, NULL);
	  if (startswith (p->name, "$SYSROOT"))
	    p->name = concat (sysroot, p->name + strlen ("$SYSROOT"), NULL);
	}

Listing 1.12-9: the = rewrite (gcc/incpath.cc:333-339), applied to the quote, bracket, system, after and #embed chains at :356-364.

The user_supplied_p test limits the rewrite to paths you passed on the command line, so it never touches the compiled-in table. To check the C++ headers in a sysroot against such a compiler, drop the compiled-in C++ directories and name the sysroot's own:

$ arm-linux-gnueabihf-g++ --sysroot=/arm-rootfs \
    -nostdinc++ -isystem =/usr/include/c++/15 -E -v -x c++ /dev/null

Listing 1.12-10: -nostdinc++ drops the compiled-in C++ directories (Chapter 1.10), = supplies sysroot-relative ones.

The -v output then lists /arm-rootfs/usr/include/c++/15 among the search directories and none of the compiled-in C++ directories, because -nostdinc++ removed those and Listing 1.12-9 expanded the =. The command names one directory, so any other C++ header directory that your rootfs needs, such as a separate per-target one, gets its own -isystem = option.

--sysroot and libstdc++.so: searched, but the tooldir copy wins

--sysroot adds the sysroot to the link-time search for libstdc++.so, through two mechanisms, but the copy in the tooldir usually wins. The tooldir prefix of Listing 1.12-4 is not sysrooted, and it holds the file that GCC installed. A libstdc++.so in the target's root filesystem therefore rarely decides which library a cross link uses.

Sysrooted startfile prefixes. The driver registers /lib/ and /usr/lib/ (gcc/gcc.cc:1613-1618) through add_sysrooted_prefix (:8621-8630), which prepends target_system_root.

The linker is told. If ld supports it, the driver prepends %(sysroot_spec), which is --sysroot=%R (gcc/gcc.cc:1189-1191), to the whole link spec (:8554-8564). ld's own built-in search directories and any =-prefixed path in a linker script then become sysroot-relative too.

A sysroot copy wins only if one is there and it sorts before the tooldir entry, which the order of Chapter 1.5 does not do by default. Arranging that is usually the wrong move, because it leaves two candidates and a search-order question to answer.

The sysrooted prefixes sit behind a gate: a cross compiler configured with no sysroot gets no /lib or /usr/lib prefixes at all. Listing 1.5-8 in Chapter 1.5 is the gate, and that chapter explains why the gate is correct. Ports that define STARTFILE_PREFIX_SPEC take the earlier branch instead (gcc.cc:8579-8587), and that branch wins outright over the /lib/ and /usr/lib/ chain.

Configure flags that move libstdc++: --with-gxx-include-dir and others

Four configure flags move the C++ library or its headers, and each must be honoured twice: by libstdc++'s install rules and by the driver's compiled-in defaults. --with-toolexeclibdir shows what happens when only the install rules honour a flag. The install manual documents --with-gxx-include-dir and --enable-version-specific-runtime-libs on the same Configuration page as --with-toolexeclibdir, but not the "Follows --sysroot?" column:

FlagMovesDefaultFollows --sysroot?
--with-gxx-include-dir=DIRlibstdc++ headers$prefix/$target_alias/include/c++/$veronly via Listing 1.12-7
--with-gxx-libcxx-include-dir=DIRlibc++ headers, and gates -stdlib=$prefix/$target_alias/include/c++/v1same mechanism
--enable-version-specific-runtime-libsheaders and libraries into libsubdiroffnever
--with-toolexeclibdir=DIRwhere cross-built libraries install$tooldir/libnever

--with-gxx-libcxx-include-dir. Besides naming the libc++ header directory, the flag decides whether -stdlib= exists at all: =no disables the option, a path enables it, and leaving it unset enables it only on recent Darwin (gcc/configure.ac:255-274). If -stdlib=libc++ is rejected as an unknown option, this flag is the reason, and the -lc++ and -lc++abi emission of Chapter 1.11 is unreachable without it. The libc++ header entry is a separate row in the same include table, distinguished by cplusplus == 2 (cppdefault.cc:58-62) and selected by flag_stdlib_kind at incpath.cc:176-177.

--enable-version-specific-runtime-libs. The flag moves both halves out of the tooldir into GCC's versioned directory: headers to ${libsubdir}/include/c++ and libraries to ${libdir}/gcc/${host_alias}/${gcc_version}$(MULTISUBDIR) (acinclude.m4:771-782). It applies only if you did not pass --with-gxx-include-dir, as the && in Listing 1.12-1 shows. A version-specific layout can never be sysroot-relative either, because Listing 1.12-7 is in the branch it does not take.

--with-toolexeclibdir. The flag moves the install but not the search, so it can produce a working build and a broken compiler. libstdc++'s configure consumes it (acinclude.m4:790-797), and the library installs where you said. Listing 1.12-4, however, builds the driver's prefix from tooldir_prefix and the target triple, and this flag does not parameterise it. If you point the flag somewhere unusual, the build installs libstdc++.so there and g++ then fails to find it, unless you also supply -L or -B.

Checking libstdc++ and <vector> with -print-file-name and -v

-print-file-name=libstdc++.so and -v -E answer the two questions about the C++ runtime on an installed toolchain: which file the -l resolves to, and which C++ header directories the compiler searches. Listing 1.12-11 puts them in a sequence with four other queries, to run when a link picks up the wrong C++ runtime:

$ arm-linux-gnueabihf-g++ -print-sysroot                  # empty means: none configured
$ arm-linux-gnueabihf-g++ -print-search-dirs              # raw prefix lists, pre-expansion
$ arm-linux-gnueabihf-g++ -print-file-name=libstdc++.so   # which file the -l resolves to
$ arm-linux-gnueabihf-g++ -print-file-name=libstdc++.a    # ditto for -static-libstdc++
$ arm-linux-gnueabihf-g++ -mcpu=cortex-a9 -print-multi-os-directory
$ arm-linux-gnueabihf-g++ -v -E -x c++ /dev/null          # the C++ header search list

Listing 1.12-11: the six questions, in the order you should ask them.

The GCC manual documents the -print-* options under Developer Options, down to the empty answer from -print-sysroot when no sysroot is configured. -print-file-name echoes its argument back unchanged when the file was not found (Chapter 1.5), so libstdc++.so coming back verbatim means "not found", not "found in the current directory".

In the output of -v -E, check whether the include/c++/… lines begin with your sysroot path. If they do, GCC was configured with a --with-gxx-include-dir inside --with-sysroot. If they do not, no --sysroot value will move them.

-print-search-dirs prints prefixes before multilib expansion, so its output is not the set of directories that a given -mcpu searches. The four-way expansion of Chapter 1.6 happens later, inside for_each_path (gcc/gcc.cc:2778), with the variant that set_multilib_dir chose (:9764). -print-file-name performs the whole search, so its answer is the one to trust.

Three things to carry away

Neither half of libstdc++ is in the sysroot, by design. The headers and the library are found relative to the compiler's install.

--sysroot moves the C++ headers only if configure decided so. That needs --with-gxx-include-dir with a path that starts with the --with-sysroot value.

The sysroot's libstdc++.so usually loses. The copy GCC installed sits in the tooldir, which is not sysrooted and is searched before the sysroot's directories.

Things that surprise people

$prefix/$target/lib is not sysrooted, is where a cross libstdc++ lives, and is searched regardless of --sysroot.

-B does not move C++ header search. It moves GCC's own headers, which is enough to change <stdint.h> and not <vector>.

A cross ignores LIBRARY_PATH. The guard is *cross_compile == '0' (Chapter 1.9). Do not debug a cross with it.

A successful link says nothing about whether the program runs. Link-time search and run-time search are unrelated systems. -L and the prefix lists resolve the -l, while DT_NEEDED, DT_RUNPATH, ld.so.conf and LD_LIBRARY_PATH decide whether the target finds libstdc++.so.6 at exec time. Getting the first right tells you nothing about the second.

Documentation coverage

Three of the four configure flags are on the install manual's Configuration page (gcc/doc/install.texi), where --with-toolexeclibdir is at install.texi:2705-2707. --with-gxx-libcxx-include-dir is in no manual in GCC 15.2.0. The search-path ordering is in the internals manual's Driver node (gcc/doc/tm.texi, "Here is the order of prefixes tried for startfiles").

What is not documented:

  • --with-gxx-include-dir's interaction with --with-sysroot. The case table of configurations exists only as the shell code in Listing 1.12-7, and configure --help does not hint at it.
  • That the sysroot decision is per include directory, carried in the add_sysroot field (Listing 1.12-5) and therefore unchangeable by any flag. The corollary, that GCC's own directories are relocated rather than sysrooted (Listing 1.12-8), is the precise reason --sysroot cannot move them, and the source is the only place that states it.
  • That --with-toolexeclibdir moves the install without moving the search. The option is documented, but the asymmetry is not.
  • That the path logic is duplicated across three files. Only the in-tree comments say so, and they are the only warning you get.

Next: Chapter 1.13, What -flto does to the link, what the option adds to the link line and who runs lto1.