What happens when you type gcc main.c

Issam Sebri
2 min readFeb 5, 2020

--

Photo by Chris Ried on Unsplash

From readable program to an executable code, how does it happen? moving from a high level language to a lower “metallic” one, which path our idea went through?

Well, it’s a four steps process guided by the Gnu GCC compiler. In order to decipher some compilation basics, we are going with a real simple program to print the “Hello, Holberton!”.

The source code

The source code is the fundamental component of a program, written by a specific programming language as we did in the example below using the C, that presents the first input in the compilation process where the file is marked with “.c” extension.

file hello.c /* hello.c: C source, ASCII text */

Preprocessor

The preprocessor is the first step from the compilation that takes the source code and eliminate comments, prepares all the header and external requirement and changes the macro with his values. It’s possible to see the affect of the preprocessor directly using the -E flag

gcc -E hello.c > preprocessed_hello

Compiler

The next stage, the compiler takes a preprocessed program and transform it to the Assembly language; a low level language known by the sequential structure form. Since ASM is very close to the machine comprehension it depends on the processor architecture. So to print out our assembly language for the program we use -S flag (may be we use -masm to specify the dialect).

gcc -S hello.c

Assembler

Each assembly sequence converted into a machine code using an utility program referred to us as assembler; is a translator to the operation code “opcode” typically handles the constant calculation and resolving data location.

Linker

The linker is the final step of compilation, it takes care of gathering all the execution requirement as object files and libraries and joining them to one single executable file.

Therefore, accomplishing this four steps procedure allow us to track the hidden hard work done by the compiler and retrace the path taken by a simple program to come to life.

At the end, we have to thank the father of the operating system Gnu Richard Stallman for this work of art.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Issam Sebri
Issam Sebri

No responses yet

Write a response