0.5 - Introduction to the Compiler, Linker, and Libraries
Compiling Source Code
In order to compile C++ source code files, the C++ compiler sequentially goes through each source code (.cpp) and does two important tasks:
- The compiler checks your C++ code to make sure it follows the rules of the C++ language.
- Second, the compiler translates your C++ code into machine language instructions. These instructions are stored in an intermediate file called an object file. The object files also containers other data that is required or useful in subsequential steps (including data needed by the linker and for debugging).
Linking Object Files
After the compiler has successfully finished, the linker kicks in. The linker performs 4 important steps:
- The linker reads each object file generated by the compiler and make sure they are valid.
- The linker ensures all cross-file dependencies are resolved properly. For example, if you define something in one
.cppfile, then use it in a different.cppfile, the linker connects the two together. If the linker is unable to connect a reference to something with its definition, you’ll get a linker error. - The linker typically links in one or more library files, which are pre-compiled collections of code that provide useful functionality.
- Finally, the linker outputs the desired output file.