The Four Stages of Compilation (C programming and gcc compiler)

Have you ever wondered how the gcc compiler works? It just takes four steps to translate source code that is written in high level machine code to a low level machine code. I’m going to show you today the four stages of compiling a C program with gcc!

Let’s start with the first stage: Preprocessing

  • Removes comments from the source code.
  • Macro expansion.
  • Also expansion of header files.

When you preprocess a c source file, it will generate a file with an .i extension. You can do this by using the following command: gcc -E file.c (the result is redirected to the standard output)

Now let’s move on to the next stage: Compilation

  • This is where the compiler comes in. It takes in the temporary file from the preprocessing stage (with the .i extension).
  • It translates the file into assembly language.
  • It also checks the C language syntax for errors.

When you compile it, it will generate a file with the extension .s and the command line for it is: gcc -S file.c

Now, we have the next step which is the Assembler

  • It will take in the last code (.s extension file from compilation) and will translate it into low-level machine code.
  • It will then generate a file with a .o extension with: gcc -c file.c

And last but not least: Linker

  • It will take in the .o extension file that was generated by the assembler.
  • It will link the functions with their original definition so that the function printf() gets linked to its original definition.
  • Then it will generate the executable file with gcc -o executablefile file.c