What happens when you type gcc main.c
GCC is a free software package capable of compiling various programming languages, including C, C++, Objective-C, Java, Ada and Fortran.
A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file).
The source code
Source code is text that presents the instructions that make up a program in a readable form, as written in a programming language.
Compiling steps
In order for our main.c code to be executable, we need to enter the command “gcc main.c”, and the compiling process will go through all of the four steps it contains. Of course gcc has options that allow us to stop the compiling process after each step. Let’s take a look at them.
1/ Pre-processor
The preprocessor gets rid of all the comments in the source file(s), includes the code of the header file(s), which is a file with extension .h which contains C function declarations and macro definitions, replaces all of the macros (fragments of code which have been given a name) by their values.
To stop the compilation right after this step, we can use the option “-E” with the gcc command on the source file.
2/ Compiler
The compiler will take the preprocessed file and generate IR code (Intermediate Representation), so this will produce a “.s” file.
To stop the compilation right after this step, we can use the option “-S” with the gcc command on the source file.
3/ Assembler
The assembler takes the IR code and transforms it into object code, that is code in machine language (i.e. binary). This will produce a file ending in “.o”.
To stop the compilation right after this step, we can use the option “-c” with the gcc command on the source file.
4/ The Linker
The linker creates the final executable, in binary.
By default, after this fourth and last step, that is when you type the whole “gcc main.c” command without any options, the compiler will create an executable program called a.out, that we can run by typing “./a.out” in the command line.