The compilation process
Compilation process overview
Let us get a look at what happen when we click on the button to compile (build) a project.
At first, what is called the preprocessor takes action. Its job is to prepare the code so the compiler can compile (translate) it into binary code. Lines of code that start with a # are called preprocessor directives. As an example, the line #include <iostream> that we saw in the default code inside the file main.cpp is, when the preprocessor is executed, replaced by the content of the file iostream.
The compiler compiles (translates) the code of all source files (Previously transformed by the preprocessor) into assembly code. Assembly code is almost like binary code written in words (Each instruction written in assembly is what is executed on the processor). Each processor architecture has its own set of instruction and therefore assembly language is not portable, unlike C/C++.
If the code contains errors, the compilation process will fail and error messages will appear. However, the error explanations are sometime hard to understand, especially for beginners. Here is an example of what the compiler could tell is if it detected a syntax error:
In that case, the error was that there was a semicolon ; missing at the end of the line number 7. The line contained the code return 0 while it should have been return 0;.
The compiler can also show us warning messages. They, however, do not cause the compilation process to fail. We can see warnings as recommendations made by the compiler. Here is a warning the compiler could give us:
That warning means that we defined (created) a variable (named n), but we did not use it in the program. The compiler warns us that maybe we forgot to use it or that maybe we do not need it so we should erase the line where it is defined.
Now that the compiler successfully compiled the C/C++ code, in every source file, into assembly code, the assembler assembles (translates) the assembly code into binary code. The result is the creation of a binary object file (Extension .o) for every source file.
Then, a program called linker takes every binary object file produced, and assembles and links them into one executable file.