What happens when you type gcc main.c?

Abidi Ghofrane
2 min readSep 18, 2019

What are all the steps of compilation?

How the Compilation Process Works for C Programs?

First of all, C is a compiled language which means it has to be compiled to be executed eventually. And we use the command gcc to compile c files.

4 Steps:

preprocessing, compiling, assembly, linking;

1-Preprocessing

Preprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:

  • removing comments
  • expanding macros
  • expanding included files

If you included a header file such as #include <stdio.h>, it will look for the stdio.h file and copy the header file into the source code file.

The preprocessor also generates macro code and replaces symbolic constants defined using #define with their values.

2-Compiling

is the second step. It takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor.

3-Assembly

Assembly is the third step of compilation. The assembler will convert the assembly code into pure binary code or machine code (zeros and ones). This code is also known as object code.

4-Linking:

Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code.

In static linking, the linker makes a copy of all used library functions to the executable file. In dynamic linking, the code is not copied, it is done by just placing the name of the library in the binary file.

how to do it?

To compile our program with gcc, we will run gcc testing123.c. Using the -o option will allow us to specify the name of our output file. Otherwise, the default executable for gcc is a.out.

Enjoy Linux :)

--

--

Abidi Ghofrane

Software engineering student at Holberton School Tunis