Compilation Process in C Explained
Mateo Garcia
Data Engineer | Python | Databricks | Spark | Azure Synapse Analytics | AWS
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.
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:
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!