Linking

The linking step is usually done with the help of ld. ld combines a number of object and archive files, relocates their data and ties up symbol references. ld normally accepts the names of different object files as input and combines them to create executable file. In the process of combining the object files, linker would combine the data and text sections of the object files, calculates the addresses of different symbols (such as printf) and replace their relative addresses with the absolute addresses.

Normally when a programmer includes a system header file such as printf, several other symbols (variables and functions) are also included with that header file. These symbols are distributed in several different libraries. Therefore the programmer ends up in providing names of million and a half object files as parameters to the linker e.g. to create a output file the linker has to be invoked with the following parameters

ld /usr/lib/gcc-lib/i386-redhat-linux/2.96/collect2 

-m elf_i386

-dynamic-linker /lib/ld-linux.so.2

/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o

/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o

/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o

-L/usr/lib/gcc-lib/i386-redhat-linux/2.96

-L/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../..

test.o

-lgcc

-lc

-lgcc

/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o i

/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crtn.o

-o test

Fortunately, gcc comes to the rescue and can resolve the required libraries for the standard header files. The output file can be created using gcc in the following way :

gcc test.o -o test