Compilation Process of C Program

Compilation Process of C Program

What is a compilation?

The compilation is the process of converting source code into object code. Your c compiler helps to do so. The compilation process is composed of 4 steps.

  • Preprocessor
  • Compiler
  • Assembler
  • Linker

YouTube Video Explaination

Source Code.png

Preprocessor

Your source code is passed to the preprocessor, now the preprocessor expands this code. Later this expanded code is passed to the compiler.

Compiler

Compiler converts your expanded code to the assembly code.

Assembler

Now assembler converts assembly code into the object code.

Linker

Every c program use library functions. These library functions are pre-compiled and the object code of these files is stored with .lib or .a extension. The linker combines the object code of library files with object code or our program. Linker also helps us to connect functions defined in some other files. After all the processing linker gives us executable file. Executable files are OS dependent e.g .exe for DOS and .out for unix based system.

Consider this example

Let say we have a code hello.c

#include <stdio.h>  
int main()  
{  
    printf("Hello from CodeWithRish");  
    return 0;  
}

It's flow will be as shown in the diagram.

C Program.png

Generate all files

Type following commands gcc -c -save-temps hello.c will generate hello.i, hello.o and hello.s now type gcc -o hello hello.c this command will generate hello.exe finally just type .\hello.exe will give program out i.e. Hello from CodeWithRish!

That's how you a C Program is compiled in background.

Did you find this article valuable?

Support Rishabh Kumar by becoming a sponsor. Any amount is appreciated!