Compilation in C - A BASIC OVERVIEW
Sabyasachi Datta
Master's in Technology (M.TECH) at ASE, Amrita Vishwa Vidyapeetham, Bengaluru.
In Today’s post, I want to talk about program compilation and the compilation process of any C program.?
I am dividing the entire article into the following questions:?
So, let us address the 1st and most important question :
“What is compilation and why is it necessary?”
When you write a program, in C, you are basically writing it in a way such that humans can understand.?
The reason these high/mid level languages exist at all is because of the fact that it is convenient for us, rather than using complex assembly code(which is also architecture specific).
So in order to convey, understand and propagate the logic that we want to write in our program, amongst humans, we use languages such as C/C++ etc.
Well, the problem comes when the machine tries to make sense of the program.
?You see, a computer only understands machine language( 0s and 1s), and hence it cannot understand the program that we have written in our computer programming language (e.g C programming language).?
Hence, the program that was written in C needs to be translated and made understandable to the machine. This entire process is called compilation.?
This brings us to our next question,
“Who does compilation?”
A tool known as the “Compiler” does the compilation. (sounds rather obvious!)
领英推荐
Well, what is a compiler??
A Compiler is a tool which is responsible for the syntax checking, variable declaration errors , type mismatches, code optimisation and so on.
The outcome of the compilation process gives us an executable code in the form of a .exe file.
Hence it is converting our source code ( .c ) into executable (.exe / .out ) .
There are multiple types of compilers like GNU GCC , MinGW , CLang etc.
I want to briefly talk about the compilation steps in a C program, which brings me to the 3rd and final question,
?“How does compilation occur/what is the process?“
After you’ve written a C program , Upon clicking the “run” button on any editor, the following steps sequentially occur?
2. The next step is to compile filename.i and produce an intermediate compiled output file “filename.s”. This file is in assembly-level instructions. [COMPILER]
3. The file which is present in the assembly code needs to be converted to machine level code, which is done by the assembler. The file format in this case is “filename.o” [ASSEMBLER]
4. The fourth stage is the linker stage, where functions implemented in external files are basically linked with the corresponding function calls. It also passes command line arguments required for setting up the environment. [LINKER]?
5. Loading is the last stage where the final file gets loaded into the primary memory, which is then accessed by the CPU for execution. [LOADER]