Here’s How to explain The CompilationProcess!!

Do you want to learn about all the steps of compilation?

Hi there, my name is Maroua. In my blog, I will try t to explain you the concept of the compilation. So, we can first ask some questions: What should you understand the compilation process? What are the steps of compilation? What happens when you type gccmain.c? So let’s Start!!

What you should understand the compilation process?

Understanding the Compilation process in C programming language helps the programmer to better understand how the executable files are created. A programmer can halt the process at certain levels if need be. If something requires a file to be stored in assembly code; the compilation of the source code could be stopped just after it reaches the assembler.

What are the steps of compilation?

Compiling a C program is a multi-stage process. At an overview level, the process can be split into four separate stages: Preprocessing, compilation, assembly, and linking.

Step One: Pre Processor

Create source code in a code editor (emacs or vi for example). The source code should be organized and easy for other programmers to understand. Use comments to provide details and information regarding the purpose of the source code. As part of the process pre processor removes the comments, includes source header in code and replaces all macro names with their values.

Step Two: Compiler

Pre-processed code enters the compiler. The compiler generates assembly code using mnemonics. The assembly code is a code the assembler can read.

Step Three: Assembler

The Assembler converts the assembly code into objects code. The object code is binary or machine language. Machine language is a code the linker can read and process. If a programmer wanted to store the code in assembly language the programmer would stop the process here and redirect the code to be stored in a file. We will continue on.

Step Four: Linker

At this stage, the Linker is able to take the machine language and link it to any references the code may call from the libraries referenced in the code and available to the linker. The Library, in this case, contains function code. Linker will decide which functions from the library are necessary and useful to covert the code into an executable file.

Here, there is a picture that illustrates the compilation process.

What happens when you type gccmain.c?

GCC in capitals is the abbreviation for the GNU Compiler Collection and it supports languages like C and C++. Now in all lower case, it is the GNU C Compiler. It will compile your C code making it object code, also called machine code. Now the GCC compiler is a translator software tool to make human readable code into machine code. A translator is also referred to as a compiler, assembler, and linker.

Example with GCC

To understand the four stages of compilation namely preprocessing stage, compiling stage, assembling stage and linking stage. Let’s take an example of a program named “hello_world.c” to understand each stage.

COMPILATION STEPS WITH GCC IN LINUX

First lets perform pre-processing on the above c program. To perfom pre-processing step on above code, execute this command in linux command line:

You will get output after pre-processing as shown in the code below. Two important things to note here is that:(Header files inclusion) First, pre-processing includes all the header files along with their relative path where they are stored in your machine.( Macros expansion) Secondly, pre-processing actually replaces the macros, such as x and y, with their actual define files.

The second step is to perform the compilation. This GCC command is used for the compilation of c program. This commands converts add.i pre-processed file (see the last step) into the assembly file.

The output of compilation step is assembly file with the name of add.s.

The third step is to convert assembly file (add.s) into object file. This gcc command converts assmbly file (add.s) into object file.

After the compilation process, the assembler generates instruction codes according to underlying instruction set architecture of your machine such as ISA32, ISA64. Finally, the linker converts the object file (add.o) into an executable file (add.exe). But linking processes also resolves the addresses of the external functions. For example, we have used external function print() in the above example code. The assembler does not resolve the address resolution of these external function and it is the responsibility of Linker to perform this task.

This command performs linking process

Now type this command to execute code from linux command line:

You will get this output:

Conclusion:

The compiler is a program that converts source code into executable. There are four stages of the Compilation of c program.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store