Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, November 7, 2020

C Compilation process

Compilation Process | welcome2protec

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) 

Compilation process | welcome2protec


The entire C compilation is broken to four stages.

  1. Pre-processing
  2. Compilation
  3. Assembling and
  4. Linking

The below image describes the entire C compilation process.

Compilation Process | welcome2prtec


To take a deep dive inside the C compilation process let’s compile a C program. Write or copy below C program and save it as compilation.c.

/* 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:

Finally, the linker comes in action and performs the final task of compilation process. It accepts the intermediate file <file-name.o> generated by the assembler. It links all the function calls with their original definition. Which means the function printf() gets linked to its original definition.

Linker generates the final executable file (.exe in windows).






Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us