What happens when you type gcc main.c
Before explaining each step of the process, we will answer the meaning of this command
GCC — GNU Compiler Collection
Before it originally meant “GNU C Compiler”, it is an integrated compiler of the GNU project for C, C ++, Objective C, and Fortran; it is capable of receiving a source program in any of these languages and generating a binary executable program in the language of the machine where it has to run.
Now we will show everything our source code file (.c) will have to go through to finally generate an executable file. We call this tje compilation process
Preprocessor
Our source code goes through the preprocessor and what we have done in main.c has made its first change. With the following command we will see this:
gcc -E main.c
This is what the gcc manual tells us about the -E flag:
Compiler
After having gone through the preprocessor, the turn is now for the compiler.
We will execute the following command, with the -c flag (in lowercase)
gcc -c main.c
As a result we have created a main.o file that we can see with the following command:
cat main.o
This is what the GCC manual tells us about the -c flag:
Assembler
After the above, our code will go to the Aseembler language. We will see it with the following command:
gcc -S main.c
With the following command we will see the content of the generated file, which in this case by default is main.s
cat main.s
This is what the GCC manual tells us with the -S flag:
Excecutable file
Finally we see the resulting product of our code, it really does nothing but we will specify with the -o flag the name of our output file with the following command:
gcc main.c -o donothing
In the same way as the previous times, we will see the content of the executable file donothing with the following command:
cat donothing
We will see that the gcc manual does not say about the -o flag:
To run the program, we just put ./ followed by the name of the executable, in this case it is donothing