Kevin Adrián Giraldo Valderrama
Holberton SchoolTasks
4 min readSep 18, 2019

--

What happens when you type ‘gcc main.c’

Compiler diagram

When you want to execute code to create an object file, the computer needs to be communicated with in machine language, which is (base-2) binary code. Unfortunately (and fortunately), humans communicate in language higher than binary. Hence, why we use a higher-level language like Python, Ruby, and in this case, the C programming language. But, in order for computers to execute our C code, we have to compile the code using the Unix command:

GCC main.c

Before continuing with the compilation process let’s stop to understand the tool with which we can compile: GCC.
The acronym GCC stands for GNU Compiler Collection. It is an integrated compiler of the GNU Project for C and some other languages; it is capable of receiving a source program in any of these languages and generating an executable binary program in the language of the machine where it is to be run.

The steps from the program in source code until it can be executed are the following:

** At the end you will find an example **

Step 1. Preprocessor

When we run gcc main.c, main.c program gets processed by preprocessor. There are three things that preprocessor does:

  • First: it removes all the comments from our program main.c
  • Second: it includes code from header and source file
  • Third: it replaces macros (if there are any that are used in the program) with code.

To get the result of compiling a ‘.c’ file to the preprocessor part, we use the tag -E

Step 2. Compiler

The code generated by preprocessor is then given to compiler and compiler generates assembly code from it.

To get the result of compiling a .c file to the compiler part, we use the tag -C

Step 3. Assembler

Since our computer still can not understand assembly code assembler converts assembly code into binary code (the one our machine can actually interpret).

To get the result of compiling a .c file to the Assembly part, we use the tag -S

Step 4. Linking

Now linker is the one that links all this binary code form our program to libraries and puts it together into one code that we get in a form of executable file (a.out).
But of course we don’t want to have all of our programs be named the same, do we? So we can use -o option with our gcc command that is allowing us to name our executable file whatever we want (for example tic_tac_toe).

To have the final executable and as previously mentioned, we use the tag -o

Example

I have the following program that prints ‘Hello World’:

Let’s see what is the result of the first compilation step: Preprocessing. For that we compile with the tag -E.

Image of the last 15 lines.

Although the previous one shows something different from the original, it can still be read and understood. The next step is no longer the case as it looks totally indecipherable.

The next step is the one in charge of converting from C language to Assembly

The last step is the one that allows us to relate all of the above and leave us with an executable result:

To deepen the GCC commands, I invite you to visit the MAN PAGE of the command.

--

--