What happens when you type gcc main.c

Sebastian Orozco Marin
2 min readJun 11, 2020

--

We use this command to compile our c script an end up with an executable file that is going to perform our desired task, but have you ever asked what happens when we compile a file? well, we are going to explain the whole process.

What a compiler does ?

The main function for the compiler is to take high level code wrote by a human an translate this documents into a language that is executable by a machine, this language is called assembler and is based on binary code, the following image show this process.

Source:https://binarymove.com/2018/12/01/how-c-works-ides-compilers-linkers/

There are several subprocess that needs to be done to have an executable, those steps are the following:

  1. Preprocesing: In this step we create a file with that contains the code of the headers, the comments are erased ans all the macros are replaced with the code, this steps prepares all our code for the following step.
  2. Compiler: in this step our file is translated into assembly code, this code is a more low level language and there is a strong correspondence between the instructions wrote and the actions that the machine has to do. We can see an example on the following image.
example of assembly code. The extension of this code is .s

3. Assembler: in this steps our assembly code is translated into binary code (assembler), a language that is readable for our machine, an his extension is .o file refering to an object code.

source: https://www.youtube.com/watch?v=VDslRumKvRA

4. Linker: in this step our object file and the libraries needed for the executing are merged together into an executable, that is our final product to do the task.

As we saw there are several interesting processes that happens to compile a C program, i hope those steps are more clear for you now.

--

--