What happens when you type gcc main.c
We use this command to compile our c script an end up with an executable file that is going to perform our desired task, but have you ever asked what happens when we compile a file? well, we are going to explain the whole process.
What a compiler does ?
The main function for the compiler is to take high level code wrote by a human an translate this documents into a language that is executable by a machine, this language is called assembler and is based on binary code, the following image show this process.
There are several subprocess that needs to be done to have an executable, those steps are the following:
- Preprocesing: In this step we create a file with that contains the code of the headers, the comments are erased ans all the macros are replaced with the code, this steps prepares all our code for the following step.
- Compiler: in this step our file is translated into assembly code, this code is a more low level language and there is a strong correspondence between the instructions wrote and the actions that the machine has to do. We can see an example on the following image.
3. Assembler: in this steps our assembly code is translated into binary code (assembler), a language that is readable for our machine, an his extension is .o file refering to an object code.
4. Linker: in this step our object file and the libraries needed for the executing are merged together into an executable, that is our final product to do the task.
As we saw there are several interesting processes that happens to compile a C program, i hope those steps are more clear for you now.