C++ : The Compilation Process

 

  • For each .cpp or .c file in your project, the preprocessor copies in all the .H files that are #included and creates a very large temporary file.

  • Then, each expanded temporary file (from the preprocessor) is compiled into an object (.o) file, which contains the methods and data in the .C file in a format that can be executed. There is also a table of symbols (variables and functions) that are referenced but not defined in this particular .o file. (e.g. C Library functions, stuff from the support code, et cetera)

  • In order to resolve all of these symbol references, the final step is to link the .o files together into an executable. The linker needs all of the .o files and any external libraries (e.g. libcs123.so) that have any referenced but unresolved symbols. The executable is basically a concatenation of your .o files with some information about external libraries.

  • When you run your program, any external libraries that were linked in dynamically will be (you guessed it) dynamically loaded by the OS's loader. Now your program can execute without issue.

  • So, you need to find out which files need to be recompiled, build them, then rebuild your linked object (executable) every time you make any modifications to source. To make this easier, we use IDE GUI compilers like Microsoft Visual Studio or .NET.