Previous: , Up: How the compiler works   [Contents][Index]


12.5 The linker

The final stage of compilation is the linking of object files to create an executable. In practice, an executable requires many external functions from system and C run-time (crt) libraries. Consequently, the actual link commands used internally by GCC are complicated. For example, the full command for linking the Hello World program is:

$ ld -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 hello.o -lgcc -lgcc_eh
 -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i686/3.3.1/crtend.o 
 /usr/lib/crtn.o

Fortunately there is never any need to type the command above directly—the entire linking process is handled transparently by gcc when invoked as follows:

$ gcc hello.o 

This links the object file hello.o to the C standard library, and produces an executable file a.out:

$ ./a.out 
Hello, world!

An object file for a C++ program can be linked to the C++ standard library in the same way with a single g++ command.