Steps to compiling a C program: A detailed view of the 4 internal moments when compiling.

Miguel Angel Barrera Palomares
3 min readOct 5, 2021

--

Compilation is the process by which the code in a C file “filename.c” is converted into an executable file in object code format which is understood by the machine.
This process consists of 4 steps: preprocessing, compilation, assembly and linking. Collectively called compilation.

Source code of a file in C
Source code of a file in C

Preprocessing

This is the first step, it receives the source code of a C file “filename.c” to which it removes the comments, interprets directives such as “<stdio.h>” expanding its content, as well as the expansion of macros (Alias ​​for reusable code portion), etc.
The output of this step (preprocessing) is shown by default in the standard output and stored in a file “filename.i”.

We use the gcc -E filename.c instruction to generate the source code preprocess.

You can view the preprocessed code with a code editor

preprocessed code

Compiling

It receives input the preprocessing result file “filename.i” and converts it to an assembly code file “filename.s”.

We use the gcc -S filename.c instruction to generate the asembly code.

You can view the preprocessed code with a code editor

Assembler code
Assembler code

Assembly

This stage consists of converting the assembler code resulting from the Compiling step and converting it to machine code, also called object code.
The file generated from this process is “filename.o” or “filename.obj” depending on what operating system your machine is running on.

We use the gcc -c filename.c instruction to generate the object code.

You can view the object code with a code editor

Machine code

Linking

It is the final part of the compilation process, in this the objective is to link the object code of our program as a result of the Assembly process with the object code of the library files and other files.
The output of this process (Linking) is an executable file that by default in UNIX-based systems is “a.out” and in DOS it is “filename.exe”.

We can execute the resulting file with the instruction -> ./a.out

Write comments if you find something wrong or if you want to share more information on the topic discussed above.

thanks for your support !!

--

--

Miguel Angel Barrera Palomares

I am a software developer in training, passionate about the world of computing, its constant evolution and its use for the benefit of society, education, etc 🤗