Thursday, 9 June 2016

Compilation process in C


In this post I am explaining the compilation process of C program. Compilation is a complicated process but we dont need to worry, this work is done by another software called compiler. 

To understand the complete process, open the terminal and make a directory by entering following commands

mkdir test

enter in that directory

cd test

we are now in the test directory. In this directory, create a file in which we are going to write our C program. There are many editors available to create a file, but I prefer nano. Go ahead and create a test.c file

sudo nano test.c 

This will open a file with name test.c. Remember the extension .c
 

In this file write the following program

#include <stdio.h>
int main(){
printf("my test\n");
}

save and exit from here by Ctrl-x and then y

enter ls in the same directory, It will show the files in that directory. Here  you can see your test.c file. Now we have a program written in c. This program doesnot do any thing by itself, SO we need to do something with it. In your terminal enter

cc test.c

after entering the command, check the content of your directory by using ls command. This time you will an a.out file in green colour. It is an executable file (thats why green colour) which has been generated from cc command. To execute this enter

sudo ./a.out

and you will see a text  "my text" in your terminal.  Suppose you dont like the name a.out and want a different file name for a.out. you can do this by following command.

cc -o newname test1.c

This will generate an executable with name 'newname' .  -o is called flag or option we can give to cc (o stands for output). Remember now you need to run sudo ./newname in place of sudo ./a.out

Note: You can use gcc in place of cc in all the above commands


Uptil now we had created a C program and run it with the help of some commands. Fine, But what happens when we enter those commands to terminal? In following I am tring to explain those things.

The file test.c is called source code. We write source codes and a.out file is called executable file.

Source code has to go through Four stages  to become an executable. These four stages are preprocessor, Compiler, Assembler and Linker(PCAL to remember). Preprocessor as name implies it preprocesses the source code, it means it looks for # for ex. # include. and removes comment. Lets try it

modify your test.c


create a c program


sudo nano test.c

#include <stdio.h>
int main(){
//this is a comment and it does not has any effect in this source code
printf("my test\n");
}


save this by ctrl -x and y .


Preprocessing


To see preproceesor output use following command

gcc -E test.c 
 

or you can use

cpp test.c testp

This command generates a file with name testp and you can open this file with sudo nano testp

Both this command gives preprocessor output. Observe the output and at last you will see the comments has been removed by preprocessor.

After preprocessing source code move towards compiler.

Compiling


Compiler generates an assemble  code. use following commands to generate aseembly code from your source code.

gcc -S test.c

This will create a file with name test.s and youcan open it and see its content by sudo nano test.s.

gcc -S -o mytest.s test.c

This will generate mytest.s file. These .s files are the assembly code of test.c file.



Assembling


Atfter compilation assembly code passes through assembler and assembler generates object code form assembly code.Assembler creates an object file, This object file is for our main processor(hardware). To generate an object code use following commands

as mytest.s -o mytest.o

Try to read mytest.o by sudo nano mytest.o and you will not understand anything.


Linking


Finally object code goes to  linker and linker converts object code to an executable. Linking is done by ld command (manually linking need a large command so for simplicity dont bother abiut thet right now). Our test program contains printf function and the printf.o is somewhere in our system, So linker links our test.o with print.o and creates an executable file. Linker also add some extra code to our program.

Just remember source code passes through P.C.A.L to become an executeble.

No comments:

Post a Comment

Comments System

Disqus Shortname