Do you know what happens when you type gcc main.c ?

Esteban Castaño Zapata
3 min readSep 17, 2020
source
  1. What is GCC?

GCC or GNU Compiler Collection is a free programming tool that allows a program to run on different platforms with different types of processors. In this case, will see the process to compile a C program.

When we run GCC main.c in the terminal, if we don’t specify an output file,an executable file name a.out is created

Executing this file outputs the results of the C program, in this case, it prints on the terminal the text Hello World. To have a better idea what gcc is doing, we’ll look how the compilation process its done.

2. GCC process

Compilation its done in the following four steps:

. Preprocessing: Three tasks are perform in this phase, first, it includes all the headers used in the program. In the case of our main.c file, it includes the entire stdio.h file in the source code.

The second task involves the removal of all the comments in the program, because the comments are not useful for the compiler, its only meaninful to the users.

The first line its remove by the preprocessor

The third task involves the case when macros are used in the program. if this is the case, the proprocessor replaces the macro names with the macro definitions.

To view in the terminal the output of the gcc preprocessor, the option -E is used as follows(only a small part of the output):

.Compilation: this stage its the most complex, because its the one that checks the code for errors, uses optimization options if there implemented and removes dead code. The compilation can be view using the -S option like so

Fist lines of the compilation process of the resulting assembly code

.Assembler: This is the phase where the source code its converted into a binary files. This process creates an object file that the machine can understand but its unreadable for the user.

.Linking: This last step takes the object file made by the assembler, combines it with the required libraries and creates the executable.

--

--

Esteban Castaño Zapata
0 Followers

Aspiring software engineer at Holberton School