The simplest form of linking is to compile a collection of object files into a directory, or set of directories, and then name them on the command line for the linker. This works out quite well for object modules that are to be linked into only one or two programs. For example, a C program consists of the source files main.c, inlet.c, outlet.c and genspru.c. The following sequence of commands will compile them all into object files and link them into an executable program named spinout:
$ gcc -c main.c -o main.o $ gcc -c inlet.c -o inlet.o $ gcc -c outlet.c -o outlet.o $ gcc -c genspru.c -o genspru.o
$ gcc main.o inlet.o outlet.o genspru.o -o spinout
After this series of commands has been successfully executed, the disk contains the four object files and one executable file. A simpler way to achieve the same thing is to let the compiler manage the entire process with a command like the following:
| $ gcc main.c inlet.c outlet.c genspru.c -o spinout
In either case, the final executable contains all the code from all four of the object files, along with other code from the system that the linker determines to be necessary.
Was this article helpful?