Comprehensive Compilation Process in C

Comprehensive Compilation Process in C

Understand how the compiler works in generating your executable file

If you're a beginner in programming or new to the C language, don't be scared about the topic - I'll walk you through it.

Now, here's one interesting topic you can't get tired of reading in C programming...

Why? The process is fun and you get to see how amazing your computer processes your code in C.

What is Compilation?

The compilation is a process in which the computer generates the executable file of your source code through an intermediary called "a compiler."

Here's a simple image to describe the process below...

Professional Blue and Gray Young Female Employee Using Laptop LinkedIn Post(1).png

So, are you ready to have a fun time writing some codes to generate each process?

Let's dive in...

The Compilation Stages

Recall, that the goal is to generate the executable file after passing the source file through these stages.

But, you've to create a source file called hello.c

//include the header file
#include <stdio.h>

int main(){
    printf("Hello world")
}

So, having created your source file - It's time to run through the compilation process.

Pre-processing

In this stage, there's mainly a macro substitution of all lines with "#" symbol and replaced with their values embedded in the header files e.g (#stdio.h, #stdlib.h, #define etc)

Also, all comments in the source file are removed.

The command to generate your preprocessed file is gcc -E hello.c -o hello

Note: gcc is the compiler you're going to use. _hello.c is the source file and hello is the output file to see what's happening in each stage.

Take a look at what's in the hello file using cat hello on the terminal.

prepocessor shot1.png

preprocessor shot_2.png

Compiling

In this stage, the assembly file is generated with a ".s" as the file extension...The contents of this file is still human-readable because it's written in assembly language.

The command to generate this file is gcc -S hello.c -o hello

Note: gcc is the compiler you're going to use. _hello.c is the source file and hello is the output file

compiler.png

Assembling

Here, the assembly file is converted to the object code or machine language file which the computer understands (0s & 1s) by the assembler.

The command to generate this file is gcc -c hello.c -o hello

assembler_!.png

Note: gcc is the compiler you're going to use. _hello.c is the source file and hello is the output file

Linking

This is the final stage of the compilation process in C...Here the object code is linked with any other object file and the library functions from the libraries of the C program.

This then generates the executable file which can be executed to get our final results...File extension is ".exe" or "a.out"

Note - The executable file will only be generated if your source file is free from errors or syntactically correct.

The command to generate the executable file is gcc hello.c -o hello

linking.png

Conclusion

The compilation process in C programming language shows you the abstract part of how the compiler works to process your source file into a executable file you can always run.

Here's my question to you..Have you tried this process before? Will you try it? Let me know in the comments section... Have a splendid coding week!