Compilation Process in C Explained

Compilation Process in C Explained

Explaining the overall process

Intro

C programming language is one of those that needs to be "compiled" but what does that mean? Once the .c file which the developer's code is in, it first must go through a series of steps which is the compiling process, which is done by the gcc command. This blog will do an overall explanation of the four steps the gcc command does, the general idea of each step, and the files extensions of each input\output.

Compilation Process Summary

Preprocessing

This process it receives the source code (.c file), and does a preparation process where it cleans up the code, where three things are done:

  • Comments are removed
  • Header is included
  • Macros are expanded

The output of this process is a .i file which can be obtained the the cpp command.

$ cpp hello_world.c c
        


Compilation

For a better understanding of this step, remember the final goal is for the machine to execute a series of commands and that the machine only understands binary code which is ceros and ones. So basically, this is making that code the machine will do, but in a human readable way. To do this it takes the clean up code with the .i file and constructs a human readable code, also known as an assembly code (.s file). To obtain this file, the use gcc -S.


$ gcc -S hello_world.c
        


Assemble

This assembler will pass the assembly code and transform it to binary code or the machine code. Yet this is not enough for the computer to execute it. The extension file this process produces is a .o file and can be obtained with the gcc -c command.


$ gcc -c hello_world.c
        

Linking

This is the las step of the compilation process. Here it is where the linking of the parts, or the merger occurs. It starts linking the code with the libraries that were used in the code and starts to... yes, link them.

This are the four steps of the compilation process for C programming language and an overall explanation of what happens under the hood when the compilation command gcc is called on the source file.

Happy coding!


要查看或添加评论,请登录

Mateo Garcia的更多文章

  • Go Deeper!

    Go Deeper!

    In this article, the beauty and power of recursion will be exposed and explained. The idea is to explain this concept…

  • Everything is an Object!

    Everything is an Object!

    In this article, a simple explanation of Python's Objects will be given! It's been said that in Python everything is an…

  • Hard Link vs Symbolic Link

    Hard Link vs Symbolic Link

    Similarities, differences, advantages and disadvantages between the two for Linux Operating System. What is a link? In…

社区洞察

其他会员也浏览了