Juan Sebastian Caicedo Alfonso
2 min readFeb 4, 2021

What happens when you type gcc main.c

The gcc command stands for GNU compiler collections. When we run “gcc main.c” the compiler runs the source code main.c through 4 steps we are explaining below.

  1. Preprocessor
  2. Compiler
  3. Assembler
  4. Linker

Each of these steps do a specific job in order to convert a high-level human understanding of coding, into something a computer can run and execute.

First, the preprocessor removes all comments from the code, replace any macro the source has by code, and include the native code from the headers. We can obtain this output into the standard output running the following command.

gcc -E file.c

Where file.c is the file with the source code

This output goes to the compiler, it is in charge to take all the code and transform it into assembly code, this output can be extracted into a file in .s format using the following command.

gcc -S file.c

The next step is for the assembler to interpret the assembly code and generate object code, which is a binary file the computer can interpret. We can obtain this code into a .o file running the following command.

gcc -c file.c

Finally, the object code is taken by the linker, this last steps links any library the program uses from the computer and in case there are multiples .o object files, all come together to make only one executable file. On Linux systems, this file is named a.out by default, but we can change the name of the file using the -o option.

For example, if we want a fully executable file named “the-exe” from our file.c file, we would need to run the following:

gcc -o the-exe file.c

No responses yet