1.14 -print-*, -v and -dumpspecs: asking a built compiler

GCC 15.2.0. To run the commands: an installed compiler, plus a working one to diff it against.

Something is wrong with a build. A header resolves to the wrong file, a -l picks up a library from a directory you have never heard of, or a flag you passed appears to do nothing. The temptation is to read the documentation and reason about what should happen.

Ask the compiler instead, because a built compiler reports what it did. Each chapter of Part I showed the commands that observe its own mechanism. This chapter collects them in the order that finds a fault fastest, for when you do not yet know which mechanism is at fault. Each check links to the chapter that explains what it shows, so you can start here and read only the chapter that your fault leads to.

-print-* options: which one answers which question

The table pairs each question about the driver's decisions with the command that answers it, a -print-* option or one of a few others such as -###, -v and -dumpspecs. Its last column names the chapter that shows what the source does behind each answer. Use the table when the fault already points at one decision, and the seven checks from -### to -dumpspecs when it does not. The GCC manual documents every option in it: -### and -v under Overall Options, -dM under Preprocessor Options, and the rest under Developer Options.

You want to knowCommandExplained in
What commands would this run?gcc -### foo.cChapter 1.1
Which file would -lfoo or crt1.o resolve to?gcc -print-file-name=libfoo.aChapter 1.5
Where does it look for programs and libraries?gcc -print-search-dirsChapter 1.5
Where does it look for headers?gcc -v -E - < /dev/nullChapter 1.8
Is the environment moving a list?env -u GCC_EXEC_PREFIX … gcc -print-search-dirsChapter 1.9
Is a sysroot in play?gcc -print-sysrootthis chapter
What was this compiler configured with?gcc -vthis chapter
Which target is this?gcc -dumpmachineChapter 1.2
Which ABI variant do these -m flags select?gcc -m32 -print-multi-directoryChapter 1.6
Which variants exist at all?gcc -print-multi-libChapter 1.6
What are the driver's command-line templates?gcc -dumpspecsChapter 1.3
Every predefined macro?gcc -dM -E - < /dev/nullChapter 2.6

From -### to -dumpspecs: seven checks, one mechanism each

The checks run from gcc -###, which shows whether a flag reaches the right program, to gcc -dumpspecs, which shows the templates behind each command line, and then repeat for C++. Each check rules out one mechanism at the cost of one command, so run them in order when you do not know where the fault is.

  1. Does the flag arrive? Run gcc -### foo.c and read the line for the program that should get the flag: collect2 for the link, cc1 for the compiler. A flag that is missing there was removed by a spec or never recognised. See Chapter 1.1.

  2. Which file wins? Run gcc -print-file-name=NAME for each library and startup file in doubt. An answer that is only the bare name means not found, and the owner of that file is the suspect: the sysroot for crt1.o, the prefix or -B for crtbegin.o. See Chapter 1.5.

  3. Is it the right variant? Run gcc -print-multi-directory with your -m flags, then check that -print-multi-lib lists that variant. A missing variant links against the default one, with no error. See Chapter 1.6.

  4. Is the environment involved? Diff -print-search-dirs with and without GCC_EXEC_PREFIX, COMPILER_PATH and LIBRARY_PATH. See Chapter 1.9.

  5. Is it a header? gcc -v -E prints the header search list, and gcc -H -E prints the chain of #include_next hops. See Chapter 1.8.

  6. Is it a spec? Run gcc -dumpspecs, and look for a stray specs file. --with-specs never shows in -dumpspecs, so read the configure line from gcc -v too. See Chapter 1.3.

  7. Is it C++? Ask the flag, file and header questions again, for -lstdc++ on the link line, -print-file-name=libstdc++.so, and g++ -v -E -x c++ /dev/null. See Chapter 1.11 and Chapter 1.12.

maybe_print_and_exit: two -print-* options, one answer

Every -print-* option is handled in one function, driver::maybe_print_and_exit, which prints an answer and returns before any compiling starts. A command that asks two of these questions at once therefore gets one answer, so ask each in its own command.

driver::maybe_print_and_exit () const
{
  if (print_search_dirs)
    ...
  if (print_file_name)
    ...

Listing 1.14-1: driver::maybe_print_and_exit (gcc/gcc.cc:8793-8959, abridged), called from gcc.cc:8301.

The function checks its flags in a fixed order, so the one answer comes from the option it checks first, whatever the order on your command line (Listing 1.14-1). Because every one of these options ends in this function, half of them carry the same "and don't do anything else" wording in the manual.

-print-sysroot: empty output means no sysroot

Empty output from gcc -print-sysroot means that no sysroot was configured, and that is an answer, not a failure. For a cross compiler, the missing sysroot has a further consequence: such a compiler gets no /lib or /usr/lib search entries at all. Chapter 1.5 shows the single line of code that decides this, and explains why it is correct rather than unhelpful.

$ gcc -print-sysroot

The GCC manual documents the empty answer under Developer Options, and warns that the printed path comes "possibly with an extra suffix that depends on compilation options". maybe_print_and_exit prints target_system_root, with target_sysroot_suffix appended whenever a suffix is set:

  if (print_sysroot)
    {
      if (target_system_root)
	{
          if (target_sysroot_suffix)
	    printf ("%s%s\n", target_system_root, target_sysroot_suffix);
          else
	    printf ("%s\n", target_system_root);
	}

Listing 1.14-2: -print-sysroot (gcc/gcc.cc:8876-8886).

On targets that define SYSROOT_SUFFIX_SPEC, of which MIPS is the canonical case, the answer changes with -EL or -msoft-float, so do not cache it in a build script. Chapter 1.6 explains the mechanism.

gcc -v: the configure line a compiler was built with

With no source file, gcc -v prints the version, the thread model and the complete configure line that the compiler was built with. Read that line when a check traces a fault to a decision made at configure time, such as the sysroot or the multilib set. The line is a string constant in a generated header, fixed at build time (gcc/gcc.cc:225), and the driver prints it as stored (:7710).

Configuring again in the same build tree appends : (reconfigured) and the new arguments to the old line (gcc/configure.ac:2222-2231). In either form, look in the line for the options that explain what the other checks found:

  • --with-sysroot, explained in Chapter 1.5
  • --with-specs, explained in Chapter 1.3
  • --enable-languages, and any --with-arch or --with-cpu defaults
  • --disable-multilib, which explains a suspiciously short -print-multi-lib

Diffing -### output against a working toolchain

When one toolchain works and another does not, diff their -### output rather than reading either one. -### suits this, because it prints each command line, quoted, and runs nothing. It is therefore safe on a broken toolchain, and its output diffs cleanly against a working one. With a source file, -v prints the same command lines as it runs them, plus every COLLECT_* variable the driver exports.

$ gcc -v   hello.c      # run everything, printing each command line
$ gcc -### hello.c      # print the command lines, quoted, run nothing

-### writes to standard error, so a pipe needs 2>&1. Piped through grep, or run with the flag under test, it answers narrower questions:

$ gcc -### hello.c 2>&1 | grep collect2          # just the link line
$ gcc -### -nostdlib hello.c                     # confirm what a flag removed
$ gcc -specs=my.specs -### hello.c               # confirm what an override did

To compare two toolchains, run the same command with each driver and give both outputs to diff, because the lines that differ hold the fault:

$ diff <(/opt/good/bin/gcc -### hello.c 2>&1) <(/opt/bad/bin/gcc -### hello.c 2>&1)

Three things to carry away

A built compiler answers from the same lists the real run uses. Every -print-* option answers and exits before any compiling starts.

A bare name back means not found. -print-file-name echoes its argument when the search fails, and the owner of that file is the suspect.

Diff -###, do not read it. Against a toolchain that works, the difference is the fault.

Documentation coverage

The GCC manual documents these options well. -### and -v are in Overall Options (gcc/doc/invoke.texi:1549), the -print-* family is in Developer Options (:20326), and -B, --sysroot and the search options are in Directory Options (:19484).

What is not documented:

  • That two -print-* options in one command give one answer, chosen by the order of the checks in Listing 1.14-1.
  • The gaps for each option, which the chapter that explains it lists: Chapter 1.3 for -dumpspecs, and Chapter 1.5 for -print-file-name and -print-search-dirs.

Next: Chapter 2.1, How config.gcc picks your part of a GCC checkout, the first chapter of Part II, where configure and make take the decisions that Part I observed.