How does the compilation/linking process work in C/C++?
The compilation of a C++ program consists of three steps −
- Preprocessing − In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. It handles preprocessing directives like #include, #define, etc.
- Compilation − The compilation takes place on the preprocessed files. The compiler parses the pure C++ source code and converts it into assembly code. This in turn calls the assembler that converts the assembly code to machine code(binary) as Object files. These Object files can refer to symbols that are not defined. The compiler won't give an error unless the source code is not well-formed. Syntax errors, failed overload resolution errors, etc occur in this step. Also note these object files can be used as static libraries as well.
- Linking − The linker is produces the final compilation output from the object files the compiler produced. This output can be a shared (or dynamic) library or an executable. It links the object files by replacing the undefined references with the correct addresses. These symbols should be defined in other object files or in the libraries. If they are defined in libraries other than the standard library, you need to explicitly pass them to the compiler as an argument so that they can be found and linked.
Published on 28-Feb-2018 15:52:43