What is compilation and how compilation process works in C

What is compilation and how compilation process works in C:

Compilation is the main process that requires to change the code to machine readable. Once you are done writing a C program in a .c file, it needs to be converted to executable code. Machine can’t understand what we wrote in that file. Compiler converts a human readable code to machine code or object code.

How compiler works

The compilation process works in four steps : Pre-processing, Compilation, Assembling and Linking. On each step, it makes changes to the source code to get the final object code.

Compile a C program:

Let’s try to understand the whole process with an example. Create one new file hello.c and write down the below program in it:

/* Hello World program */
#include <stdio.h>

int main()
{
    // printing hello world
    printf("Hello World !");
    return 0;
}

Here,

  • We have one comment before the program and one inside the program.
  • It will print one line Hello World ! if you run this program.

Now, open one terminal and run the below command:

gcc hello.c -save-temps -o hello

Here,

  • -save-temps is used to save all temporary files. Files those are created in the compilation process are marked as temporary files and these are deleted once the compilation is done. If we use this flag, these files won’t be deleted.
  • -o hello is used to change the output file as hello.

It will create the below files in that folder: C compiler output

  • hello.i is the file created in Pre-processing step.
  • hello.s is the file created in Compilation step.
  • hello.o is the file created in Assembling step.
  • hello is the final file created in the last linking step. This file can be executed like ./hello.

Now, let’s try to understand how these steps works in details:

Pre-processing step:

In this step, the preprocessor takes the c source code file and it expands all header files, expands the macros and remove all comments.

It generates one new file with .i extension. You can open the file in your favourite text editor to see how the content looks like.

Most of the content will be not readable. At the end, you will find the program without any header file and comments.

Compilation:

The file generated in the above step, or the .i file is taken by the compiler and converts it to assembly language. It also checks the program for any syntax errors. It creates the .s file.

Assembling:

The compiled file, .s is accepted by the assembler. It converts the code into a object code. It creates one new file with the same name as the source file. In DOS, the file has extension .obj and in Unix, it has extension .o.

I am working on a unix machine. So it created hello.o as shown above.

Linking:

This is the final step. The linker takes the output file generated in assembling and links the source code with other library files. For example, printf is defined in a different file. The object code of this file is already generated. Linker links the object code of our file with the object code of this library file.

It generates the final executable file. In windows, it can be a .exe file, in unix, it can be a.out or as shown in the above example, it is renamed to hello.

The final executable file can be run in our system.

You might also like: