What happens when you type gcc main.c

Andres Aristizabal
3 min readSep 17, 2020

When you work on your C files, in order to get them to become executable you have to do a process called compilation, but ... what really happens in this process?

The first thing we have to know is that computers only understand machine code, this language is far from the code we write in C, hence the need to “translate” the C programming language into something they understand, the famous binary code. We call this process compilation and it has several phases, here I will explain them step by step using the gcc compiler.

GCC stands for GNU Compiler Collection and it works with languages like C, C++, Objective-C, Fortran, Ada, and Go. For our example we’ll use C language.

The compilation process could be explained in 4 different stages:

  1. Preprocessing
  2. Compilation
  3. Assembly
  4. Linking

To begin to understand the compilation process more closely, let’s start with the following example:

Stage 1. Preprocessing

The result of this stage is a file with an .i extension on which the following processes have been carried out by the preprocessor:

  • First: macros are replaced with their absolute values
  • Second: comments are removed
  • Third: the required header is included in the source code itself

To carry it out, you just have to enter the following command:

gcc -E fileName.c
The file after the Preprocessing stage

Stage 2. Compilation

In this stage gcc takes the preprocessor file as input and converts this high-level code into Assembly instructions that are specific to the processor architecture of the machine on which the code will be used. The result is a file with the extension .s

To compile only up to this stage you can use the following command

gcc -S fileName.c
The file after the Compilation stage

Stage 3. Assembly

This stage converts the Assembly Code into Machine Code with precise instructions to be run by the target processor, the residual from this stage is a file with the extension .o

The code coming out might look something like this:

The file after the Assembly stage

Use this command to compile only up to this stage:

gcc -c fileName.c

Stage 4. Linking

At this stage the compiler takes all the external libraries that were defined in the header, libraries with functions as common as printf (); and incorporates them into the final file, resulting in an executable .out or .exe depending on the operating system.

To instruct gcc to achieve a complete compilation through all stages, just type gcc fileName.c but we regularly use the -o flag to indicate a new name to the output file, in this way the command would look like this:

gcc fileName.c -o newFileNam

And so we reach the end of the C compilation process with gcc, a powerful tool that transforms our high-level code into machine code to be able to be executed by our computers and thus give life to our work.

--

--