32

I'm new to linux. Can anyone explain to me the following verbose mode output for my hello world program? Also, what do the files crt1.o, crti.o, crtend.o, crtbegin.o and crtn.o and lc and lgcc do? Any other explanatory links are also welcome.

$ gcc -v hello.c

Reading specs from /usr/lib/gcc-lib/i686/3.3.1/specs
Configured with: ../configure --prefix=/usr
Thread model: posix
gcc version 3.3.1
 /usr/lib/gcc-lib/i686/3.3.1/cc1 -quiet -v -D__GNUC__=3 
 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=1 
 hello.c -quiet -dumpbase hello.c -auxbase hello -Wall
 -version -o /tmp/cceCee26.s
GNU C version 3.3.1 (i686-pc-linux-gnu)
 compiled by GNU C version 3.3.1 (i686-pc-linux-gnu)
GGC heuristics: --param ggc-min-expand=51 
 --param ggc-min-heapsize=40036
ignoring nonexistent directory "/usr/i686/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/include
 /usr/lib/gcc-lib/i686/3.3.1/include
 /usr/include
End of search list.
 as -V -Qy -o /tmp/ccQynbTm.o /tmp/cceCee26.s
GNU assembler version 2.12.90.0.1 (i386-linux)
using BFD version 2.12.90.0.1 20020307 Debian/GNU
Linux
/usr/lib/gcc-lib/i686/3.3.1/collect2
 --eh-frame-hdr -m elf_i386 -dynamic-linker
 /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
 /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o
 -L/usr/lib/gcc-lib/i686/3.3.1
 -L/usr/lib/gcc-lib/i686/3.3.1/../../.. /tmp/ccQynbTm.o
 -lgcc -lgcc_eh -lc -lgcc -lgcc_eh
 /usr/lib/gcc-lib/i686/3.3.1/crtend.o
 /usr/lib/crtn.o
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Gomathi
  • 973
  • 7
  • 17
  • 37

1 Answers1

50

The first part is the version and configuration data for the compiler driver (that's the gcc binary, which is not actually the compiler itself):

Reading specs from /usr/lib/gcc-lib/i686/3.3.1/specs
Configured with: ../configure --prefix=/usr
Thread model: posix
gcc version 3.3.1

Then it prints the command it uses to call the real compiler, cc1:

 /usr/lib/gcc-lib/i686/3.3.1/cc1 -quiet -v -D__GNUC__=3 
 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=1 
 hello.c -quiet -dumpbase hello.c -auxbase hello -Wall
 -version -o /tmp/cceCee26.s

And cc1 prints it's version and config info.

GNU C version 3.3.1 (i686-pc-linux-gnu)
 compiled by GNU C version 3.3.1 (i686-pc-linux-gnu)
GGC heuristics: --param ggc-min-expand=51 
 --param ggc-min-heapsize=40036

Then cc1 tells you what directories it will search for include files.

ignoring nonexistent directory "/usr/i686/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/include
 /usr/lib/gcc-lib/i686/3.3.1/include
 /usr/include
End of search list.

The compiler is now complete, so gcc tells you the assembler command it will use.

 as -V -Qy -o /tmp/ccQynbTm.o /tmp/cceCee26.s

And the assembler, as, gives its version info.

GNU assembler version 2.12.90.0.1 (i386-linux)
using BFD version 2.12.90.0.1 20020307 Debian/GNU
Linux

The assembler is now done so gcc gives the linker command. It's using collect2 as an intermediary to the real linker ld, but that's not important here.

/usr/lib/gcc-lib/i686/3.3.1/collect2
 --eh-frame-hdr -m elf_i386 -dynamic-linker
 /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
 /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o
 -L/usr/lib/gcc-lib/i686/3.3.1
 -L/usr/lib/gcc-lib/i686/3.3.1/../../.. /tmp/ccQynbTm.o
 -lgcc -lgcc_eh -lc -lgcc -lgcc_eh
 /usr/lib/gcc-lib/i686/3.3.1/crtend.o
 /usr/lib/crtn.o

The linker gives no verbose output (try -Wl,-v), so that's it.

The "crt" files mean "C RunTime". They are small sections of code inserted at the start of your program, and at the end. They take care of initialising your global variables, heap, and stack. They call atexit functions after you return from main. And some more besides.

Hope that helps.

ams
  • 24,923
  • 4
  • 54
  • 75
  • 6
    The linker does give verbose output if you set `-Wl,--verbose`. It will print the attempts to acquire all inputs, including libraries. – Interarticle Jan 09 '15 at 03:58
  • This surely deserves more than +28!? Very helpful. Thank you @ams – nickform Jan 26 '21 at 14:19
  • In my gcc output I see `Configured with: --with-gmp-lib=/usr/local/lib --with-mpc-include=/usr/local/include`, does that mean header files and libraries must reside at those locations for gcc to work properly? Can those folder locations be overridden? – raffian May 17 '23 at 00:16
  • 1
    @raffian Those were the configuration settings for the `gmp` and `mpc` libraries that are needed to compile GCC itself. The configuration settings are recorded for posterity but are not typically of importance to the end user. – ams May 17 '23 at 12:40