Step by step : Compiling C program
Ever wondered how a high level language gets converted into low level language, better to say in ‘binary code’? I did.
I choose C language to move forward with simple piece of code.
Explanation
- include “stdio.h” header(we all do)
- define a main function() of int type.
- initialized a variable number with value 6 of int type.
- a print statement using printf() function.
- a return value
This is called source code named as internal.c (C file)
How this code gets compiled?
There are various stages of compilation as follows:
Every stage will be discussed briefly:
Compiler I used: gcc ,operating system used: Ubuntu, file name: internal.c
- Preprocessor: Preprocessor replace the header files with its original value that is #include <stdio.h> will be replaced by its source code. Input to this stage → internal.c(source code) and output of this stage:
To check please run command: gcc -E internal.c
This output goes to the Compilation stage as an input.
2. Compilation: The compilation stage takes the Preprocessor stage output as a input and generates a assembly code.
To check please run command: gcc -s internal.c
A output file created called as internal.s and this output goes to the Assembler stage as an input.
3. Assembler: In this stage the actual instructions on the machine level are generated from the assembly code. It corrects the assembly code syntactically and semantically. Each architecture has its own assembler which converts the assembly to machine code. It may look like this(sort of).
Command to generate assembled file: gcc -c internal.c and file generated: internal.o(the above image is not an example of this file).
4. Linker: Linker find the functions that are being used in our program from other files or headers and link the object of our file and object of another file/header to make it work.
Example:
The definition of printf function is present in the stdio.h header, So linker will find the printf function from stdio.h and link it to our piece of code such that we can print any statement. If linker is not able to link the same the printf statement will throw an error that is “printf is undefined”.
To make an executable file
Command → gcc -o <name_you_want> internal.c
Example → gcc -o internal internal.c
It will create an executable file for the c program.
Summary:
- Preprocessor: Replace the <stdio.h> with its whole code.
- Compilation: convert the whole code into assembly language.
- Assembler: Correct the assembly code syntactically and semantically received from the previous stage.
- Linker: Linker finds the functions that are being used in our program from other files or headers and links the object of our file and object of another file/header to make it work.
Summary in Hindi(Translated by Google hence may not be fully correct)
प्रीप्रोसेसर में हमारे कोड में प्रयुक्त हेडर और फाइलों की परिभाषा शामिल है और आउटपुट के रूप में संकलन चरण को देते हैं। संकलन चरण पूरी फ़ाइल को असेंबली भाषा में परिवर्तित करता है और फिर आउटपुट को असेंबलर चरण में भेजता है।
असेंबलर चरण प्राप्त असेंबली कोड को वाक्यात्मक और शब्दार्थ रूप से सही बनाता है और उस ऑब्जेक्ट फ़ाइल को उत्पन्न करता है जिसमें मशीन कोड होता है। उसके बाद लिंकर उन कार्यों को ढूंढता है जो हमारे कोड द्वारा उपयोग किए जा रहे हैं जिनकी परिभाषा अन्य फ़ाइल या शीर्षलेख में मौजूद है और इसे काम करने के लिए हमारी फ़ाइल और किसी अन्य फ़ाइल/हेडर की वस्तु के ऑब्जेक्ट को लिंक करती है।
Thanks for reading!!!
Compilation: convert the whole code into assembly language.
Assembler: Correct the assembly code syntactically and semantically received from the previous stage.
Linker: Linker finds the functions that are being used in our program from other files or headers and links the object of our file and object of another file/header to make it work.