Steps of compilation in GCC

Martin Corredor
2 min readJun 15, 2020

GCC stands for “GNU Compiler Collection” and is an integrated compiler distribution developed for high-level languages ​​such as C, C ++, Objective C and Fortran.

Understanding the Compilation process in C programming language helps the programmer to better understand how the executable files are created in case you need to stop the process to get different file types.

There are four steps of internal processes before obtaining an executable file with gcc

To illustrate the steps below, we are going to run the commands on the same file main.c

Step 1. Pre processor

Each C source file goes through the pre processor, named cpp. In this stage, all include files and macros are expanded to get preprocessed C source code.

In other words, as part of the process pre processor removes the comments, includes source header in code and replaces all macro names with their values.

You can cut the gcc process in this step by typing in the terminal:

$ gcc -E main.c -o [file output name]

we add it “-o [file output name]” to write the name of the output file of our preference.

The file output will have extension “.i”

Step Two: Compiler

In this stage each file is compiled into assembler code. It is a complex program because there is not a one to one correspondence between C instructions and assembler instructions

Pre-processed code enters the compiler. The compiler generates assembly code using mnemonics. The assembly code is a code the assembler can read.

The output is a, human readable, assembler source file ending with “.s”

You can cut the gcc process in this step by typing in the terminal:

$ gcc -S main.c

Step three: Assembler

The Assembler converts the assembly code into objects code. The object code is binary or machine language. Machine language is a code the linker can read and process.

In this stage each file is assembled: the assembler code is transformed to an object file with “.o” extension.

You can cut the gcc process in this step by typing in the terminal:

$ gcc -c main.c

Step four: Linker

This stage is where the GCC compiler combines all the object files and libraries that were used in the source file. The result is a binary file in a format close to the object file.

The finaly result is a file with extension “.out”

To get the executable file without stopping at any step, we type:

$gcc main.c

Write by Martin Corredor

--

--