Showing posts with label GCC. Show all posts
Showing posts with label GCC. Show all posts

Monday, 17 October 2016

// // Leave a Comment

How Compiler works on C program??

Last post we seen that how to compile C program.Now we will see how the compiler working on the C programming.
As we know that compiler converting source code into machine code that we are going to execute on the hardware then we will going to get output.
But we do not know how the compiler working internally on the C program. The answer is here. First we will see the internal architecture of compiler.
There are 4 major stages when compiler compiles the C program.
These steps are


  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking
We are going to discuss these steps are in details.

1. Pre-Processing

In this step compiler will going to do
  1. Macro substitution
  2. Comments are stripped off
  3. Expansion of the included files
In this step header files we added after pre-processor tag that is #include<stdio.h>
get expanded.
Let us see the example of it.
To see the output of pre-processing stage, we need to  run following command in terminal:
gcc -E hi.c -o bye






The output of this command will create file of name bye.
open that file then you will able to see expanded code.

you can able to see your written code at the bottom of that file.
you can easily get all intermediate file by simple command :
gcc -save-temps hi.c -o bye
 This command will generate three intermediate files.
1. hi.i  
2. hi.s 
3. hi.o
Here hi.i file is output of pre-processing step.

2. Compilation

In the next step the pre-processed file will work as input to Compilation process.
now we will submit hi.i file to compilation process.
This step will convert the code into Assembly code.
so the output of this step will be hi.s
As you are able to see the hi.s is assembly code.

3.Assembly

Now in this step the hi.s file is taken as input and it get converted into object code that is hi.o
This file is object code file which contains machine code which is we can not understand.

4.Linking

This is the final stage at which all the linking of function calls with their definitions are done.
The linker also does some extra work; it combines some extra code to our program that is required when the program starts and when the program ends. For example, there is code which is standard for setting up the running environment like passing command line arguments, passing environment variables to every program. Similarly some standard code that is required to return the return value of the program to the system.

Now we are able to see what actually happening when we are running c program.
In the next post we will discuss regarding how  operating system runs c program.




Read More

Saturday, 15 October 2016

// //

How to run C program in Linux??




Most of the students are having habit of using Turbo-C to write C  and C++ programs.They think it as easy to use and easy to run. But Turbo-C software is not giving actual sense of running c program.
 When you runs c program in turbo-c you have to write getch() to block or freeze output window.
Actually you are not freezing output window that is called as incomplete execution of program.
your program is waiting to get character from keyboard to come out from that command prompt.
When you are using Linux operating system to run your C program there is need of GCC compiler that is GNU Compiler Collection is used to compile c program. Most of the time it is by default present in Linux operating system. The explanation given below is related with Ubuntu.
You can check GCC version and whether it is installed or not by using simple command on terminal.
Command: gcc -v


If It is not showing the result then you have to install it through terminal.
Command for the same: sudo apt-get install gcc

After successful installation of GCC major question is how to run your c program.
Type your c program in editor such as gedit or any other editor which you like.
Save that program with .c extension.now from your terminal go to respective directory where you saved your c program.
After that you have to compile your C program with GCC.
Command for that is: gcc hi.c -o bye
I would like to explain the command 
gcc is command for compilation. Instead of gcc you can also use cc.
hi.c is file name
-o is attribute for output 
bye is output file name.
This Command will create executable file having name "bye".

After successful compilation compiler will create executable file bye.
now we have just type ./bye to run that executable.

You successfully executed c program.
In Next post we will see what actually happen behind the screen when you compile c program.


Read More