Next: Recompiling and relinking, Previous: Compiling multiple source files, Up: Compiling a C program [Contents][Index]
If a program is stored in a single file then any change to an individual function requires the whole program to be recompiled to produce a new executable. The recompilation of large source files can be very time-consuming.
When programs are stored in independent source files, only the files which have changed need to be recompiled after the source code has been modified. In this approach, the source files are compiled separately and then linked together—a two stage process. In the first stage, a file is compiled without creating an executable. The result is referred to as an object file, and has the extension .o when using GCC.
In the second stage, the object files are merged together by a separate program called the linker. The linker combines all the object files to create a single executable.
An object file contains machine code where any references to the memory addresses of functions (or variables) in other files are left undefined. This allows source files to be compiled without direct reference to each other. The linker fills in these missing addresses when it produces the executable.
• Creating object files from source files | ||
• Creating executables from object files |