Compilation : How does it work ?

LAPEYRE Nathan
3 min readFeb 10, 2021

--

Before introducing what is compilation and what are it’s different steps, know that we will use GCC (GNU Compiler Collection) to illustrate our examples, an optimizing compiler produced by the GNU project. It support different kind of programming languages such as :

  • C (with gcc)
  • C++ (with G++)
  • Objective-C (with GobjC)

And others, it is usable on different operation systems. It was designed to replace the standard C compiler on UNIX system. Here we will use gcc with it’s different options to illustrate how compilation works.

So, what is compilation ?

The compilation is the process of translation of the source language to the target language by a compiler. In order to do this the compiler will go through different steps.

The compilation will start from our Source file, which is the file where we put our code, for example :

  • Source.c

This is the file that contains the source code in C that we want to compile to be able to execute it.

The next step is where the source code pass the preprocessor, that will expand the code (as seen in the picture above) and push it to the compilation step.

In it, the expanded source code will be converted into the assembly code, it compile the pre-processed code to give it to the Assembler.

During this step, the assembly code is translate to the object code, the object file is generated with the same name as the source file.

Finally, the last step is the linker, that will combine the object code of our file with the object code of the libraries that we will use in it, it can sometimes even call other files, at the end we will have the executable file.

Alright, but how can we use those steps with GCC ?

GCC have many options and I recommend you to see the type “man gcc” in your terminal or search it on the internet to find more informations, in here I will use only those that we need. The command to simply compile the file through all the steps is quite simple :

gcc sourcefile.c

I will recommend to use the option -o to rename the execute file, like this :

gcc sourcefile.c -o executefile

The extension of the execute file will depend of your exploitation system. But if we want to stop the compilation after a specifing process we can use different options, imagine that you only want the preprocessed source code :

gcc -E sourcefile.c

We will use the option -E, that stop right after the preprocessing stage. You can do this for the differents steps, here the others :

gcc -S sourcefile.c

Generate the assembly code but stop right after, do not assemble.

gcc -c sourcefile.c

Go through the different steps, assemble the source file but do not link it, it will result as a simple object file. There are other options like (-Wall, -pedantic, -pedantic-errors, etc…) that can be use to enable all the warnings, issues, etc… I invite you to see the full GCC manual page to know more about it.

Thanks for your reading and hope you’ve enjoyed !

--

--