C/C++ Compilation
The C/C++ compilation, or building, process contains fours stages.
Namely: Preprocessing, Compilation, Assembly, Linking
Take the following code as an example:
1. Preprocessing
Lines starting with a # character are interpreted by the preprocessor as preprocessor commands. These commands form a simple macro language with its own syntax and semantics.
Given the “Hello, World!” example above, the preprocessor will produce the contents of the stdio.h header file joined with the contents of the hello_world.c file, stripped free from its leading comment.
2. Compilation
The preprocessed code is translated to assembly instructions specific to the target processor architecture. These form an intermediate human readable language.
This will create a file named hello_world.s, containing the generated assembly instructions.
3. Assembly
An assembler is used to translate the assembly instructions to object code. The output consists of actual instructions to be run by the target processor.
Running the above command will create a file named hello_world.o, containing the object code of the program. The contents of this file is in a binary format and can be inspected using hexdump or od by running either one of the following commands:
4. Linking
The object code generated in the assembly stage is composed of machine instructions that the processor understands but some pieces of the program are out of order or missing. To produce an executable program, the existing pieces have to be rearranged and the missing ones filled in. This process is called linking.
The linker will arrange the pieces of object code so that functions in some pieces can successfully call functions in other ones. It will also add pieces containing the instructions for library functions used by the program. In the case of the “Hello, World!” program, the linker will add the object code for the puts function.