What happens when you type gcc main.c?
Have you ever wondered what happens when you create a C-based code and compile it with GCC?
very well if you asked yourself this question. This blog will help you better understand how it works.
The first step is to create a file that in this case will be called main.c, once the next step has been created is: COMPILE.
And what is compiling? the answer is simple, programming this word means that a program will translate from a high-level language, such as C, to the machine language, binary. Making the compiler, the translator program that in this case is GCC, responsible for our system to understand what our program does.
The compilation of a program, which in turn, consists of four main steps, which are: Processor, compiler, assembler and linker.
so to do all this process with our "main.c" program we must use GCC, which is called GNU Compiler Collection for its initials, and it works with languages like C, C ++, Objective-C, Fortran, Ada, and Go. in this example we will use C.
In this image we see that the file "main.c" is already created, and we see its content (an example code in C language), well followed by this, what we will do is use GCC, and show the result.
Here we see that an output file has been created, but what happened? That is what we are going to talk about next.
1. processor:
When writing the gcc command accompanied by the name of our file "gcc main.c", what gcc does first with the processor is to eliminate the comments that were in main.c, the second, add the code of the header and the source file, Third, remove the macros and put the corresponding code.
2. Compiler
This generates the assembly code from the code that the processor gave it.
3. Assembler
Since our computer still does not understand the language that is written in our program, the assembler converts it into binary code, which is the code that our machine can understand and execute. But everything does not end here, we still need a process.
4. Linker
The linker creates the links between the binary code of our program and the libraries, and in this way we obtain the executable file called a.out.
In order for our program to have the name that seems best to us, we use the gcc option "-o" which allows us to do this, like this: