Social Icons

Sunday, October 6, 2013



We have already discussed about what is GNU/C compiler. Lets see how to actually compile a program using the Gcc compiler. So, what we are looking at is to understand how to use this compiler tool chain called as Gcc. Here I am using the phrase "compiler tool -chain", because Gcc is a compiler distribution and its not just a single tool, its a combination of tools, its a collection of different tools all under a single name called Gcc. 


There are two different types of compilers. 
1. Host compiler.
2. Cross compiler.

  Host compiler: 

Host compiler is the compiler which will compile the source-code and build an executable for the present architecture i.e the architecture of the system on which the Gcc is presently installed.

Cross compiler: 

Cross compiler is used to compile the source-code and build an executable for any specific architecture.


When I am using the word  architecture, I am actually referring to the arrangement of the registers, accumulator, number of registers, the data buses used in the architecture and the different components present and how they interact with the processor and each other  etc.. 

Speaking about the computer architecture,  I am using  Intel'sX86 architecture. There are many different architectures like X86, ARM etc.. The compiler we use to compile and build differ for different architectures. 
(You will understand why do we require different compilers for different architecture in my future posts).

How to compile:

Lets now see how to compile and build a binary-executable file for a simple C- program. Here our objective is to understand how to compile a program using the Gcc compiler distribution.

Here I am going to write a simple C-program using the Vim-editor. (If you are new to Vim-editor please check out my older posts on HOW TO USE VIM EDITOR)
Here I am inside a directory called temp. I am creating a file called firstprogram.c. This will open a new file called firstprogram.c. If a file already exists with the same name in the present directory it will open that particular file. 
 This is a very simple C-program. I am printing "MY NAME IS AKHIL ASHOKAN". I am saving it as firstprogram.c The " .c " extension is very important for the compiler to recognise it as a file with a valid C-code inside it.


Now I have saved this file with the above mentioned syntax displayed, and the directory temp contains a file called as firstprogram.c 

To compile the program the syntax is  gcc <<filename.c>>  -o  <<binary-executable filename>>.
Here my input filename will be firstprogram.c  and the binary-executable filename could be anything of your choice. Look at my screen-shot.

Open the screen-shot and observe carefully the syntax. 
                firstprogram.c is the file we have created just a moment ago and I am passing that file containing the C-code to the Gcc compiler.

Here the executable binary file which contains the binary code which can be executed can be given anyname of our choice. I have given the name as "anyname". 

                  Here in the syntax " -o " means that whatever executable output  the Gcc will generate is to be pushed into the file called  anyname. Now the file anyname contains the compiled output of the program firstprogram.c

                            " -o " is a flag whic I used to instruct the Gcc to push the compiled output into the file anyname. These are also called as switches. Different developer's use different name. Here the executable file called anyname is used as a container to contain the result of the compilation process.
(note: Its the alphabet " o " and not the number " 0 "just in case you get confused).

Here you can clearly observe that now there are two files in the directory temp. One is the firstprogram.c which contains the C-sourcecode and the other file is the binary-executable file called anyname which is in green colour. The colour indicates it contains binary machine instructions.

Now the first step i.e the compilation is completed. The second step is to execute the binary-executable file. To execute the executable file the synatx is as follows
syntax:  ./ <<executable filename>> 

 Here we are executing it by mentioning the path where the executable file exists. Here the dot 
" . " means the present working directory. You can see that I executed the program by giving the command " . / anyname " and the output is displayed on the console or the terminal. Here as I am in the present working directory I mentioned the dot " . " to specify that the file anyname is in the present working directory.

As I have mentioned in my older posts there is a command called pwd to know the present working directory i.e where exactly in the file system we are currently standing. 
 Here my present working directory is /home/akhil/temp.
When I mention dot " . " it simply relates to this path. This means if we know the path where the executable exists we execute the file from anywhere by simply specifying the path and the executable filename. 

Here observe carefully what I am doing is I once executed the executable-file " anyname " standing in the present directory by mentioning " ./ anyname " and we get the output.
Now I leave the directory temp using the command " cd .. " and this time I mention the path of the executable-file where exactly it is located in the file system and execute it, we got the same output. 

                                                                 This demonstration is to clarify the significance of dot " . " and what is the need of mentioning it. 

(Note:  As I have started everything from the scratch please refer older posts in case you get any queries or you can always send me your doubt through " ASKME " in the home page. 

No comments:

Post a Comment