Compilation process of a program.
Before knowing the compilation processes of a program, we must know what is a “Compiler”
What is a compiler?
A compiler is a computer program that translates a program written in one programming language into another programming language, generating an equivalent program that the machine will be able to interpret. Usually the second language is machine language (binary), but it can also be an intermediate code (assembly example), this translation process is known as compilation. There are many compilers among which are:
Borland Turbo C, GCC, Tiny C Compiler, etc.
This time we will work with GCC, which in fact its acronyms are GNU Compiler Collection and it is one of the best known and most used compiler.
Do all programming languages have to be compiled?
No, there are programming languages that should not be compiled (they are interpretation languages); like python, ruby, among others, we will talk about these in a future post.
In this occasion we will talk about the c programming language, all the examples shown below will be in this language.
The world of compilers is a very extensive world, but the fundamental processes of compiling a program are:
Pre-processor, compiler, assembler, linking.
Let’s take a closer look at these processes.
Preprocessing: The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.
If you want to print the result of the preprocessing stage, type this:
Compilation: At this stage, the preprocessed code becomes some assembly instructions ideal to be read and understood by humans. This part of the process will make a file containing such assembly instructions with the extension (.o).
To get only the assembly code, type the following:
Assembly: Once the compilation process finishes, the compiler will proceed to convert that assembly code to machine code (binary).
To make a result file, type the next code:
When this code is executed, it will make a file with the extension (.s).
Linking: Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code. In static linking, the linker makes a copy of all used library functions to the executable file. In dynamic linking, the code is not copied, it is done by just placing the name of the library in the binary file.
The outcome of this linking process is a executable program a.out. To compile a program, use the following command:
In total these are the four fundamental processes that a code goes through (when it is written in a language that must be compiled).