In the series of C tutorial we learned some basic of C programming language, configured C compiler and learned to compile and execute C program.
Since the compilation and execution of first C program, I must answer few questions before moving ahead. Questions such as - what is meant by compilation, what happens during compilation, how a simple plain text file gets converted to executable binary file.
In this post I will take a deep dive into the C compilation process. So let's begin.
What is compilation?
The compilation is a process of translating source code (human readable) into machine code (computer executable) is known as compilation. The compilation is done by special software known as compiler. A compiler takes the source code as input (written in a high level language) then checks for any syntactical or structural errors and if source code is error free, then generates object code with extension .obj (in Windows) or .o (in Linux)
/* Learning C compilation process */
#include
int main()
{
printf("C Compilation process.");
return 0;}
To compile the above program open command prompt and hit below command.
gcc -save-temps compilation.c -o compilation
The -save-temps
option will preserve and save all temporary files created during
the C compilation. It will generate four files in the same directory
namely.
- compilation.i (Generated by pre-processor)
- compilation.s (Generated by compiler)
- compilation.o (Generated by assembler)
- compilation (On Linux Generated by linker) or (compilation.exe On Windows)
Now lets look into these files and learn about different stages of compilation.
Pre-processing of source file:
The C compilation begins with pre-processing of source file. Pre-processor is a small software that accepts C source file and performs below tasks.
- Remove comments from the source code.
- Macro expansion.
- Expansion of included header files.
After pre-processing it generates a temporary file with .i
extension. Since, it inserts contents of header files to our
source code file.
To view contents of the pre-processed file open <file-name>.i
in your favourite text editor.
Compilation of pre-processed file:
In next phase of C compilation the compiler comes in action. It accepts
temporary pre-processed <file-name>.i
file generated by the pre-processor and performs following
tasks.
- Check C program for syntax errors.
- Translate the file into intermediate code i.e. in assembly language.
- Optionally optimize the translated code for better performance.
After compiling it generates an intermediate code in assembly language
as <file-name.s>
file. It is assembly version of our source code.
Assembling of compiled source code:
Moving on to the next phase of compilation. Assembler accepts the
compiled source code (compilation.s) and translates to low level machine code. After successful assembling
it generates <file-name.o>
(in Linux) or <file-name.obj>
(in Windows) file known as object file. In our case it generates the compilation.o file.
This file is encoded in low level machine language and cannot be viewed using text editors. However, if you still open this in notepad, you can see it.
Linking of object files:
Linker generates the final executable file (.exe in windows).
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.