GCC verbose mode output explanation

2019-03-10 03:07发布

问题:

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

回答1:

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.