You are here:

C/C Compilation

Advertisement


Question
Hi,
pls give me the detailed compilation process(means different stages\steps from sourse code to executable code(1's & 0's))and brief discussion.


Answer
dear Srinivas !



A C/C++  program exhibits various phase such as editing  , compiling , linking  , and running !

The Source Code for a typical C program consists of one or more header files, which usually have the stdio .h , string.h  ....  These usually contain

declarations of variables, functions and classes( If in case C++).

Header files are either provided by the programmer or supplied with the compiler, operating system or some external package. They allow the

programmer to access standard library functions. e.g. the maths functions sin(), cos() etc.

The C/C   Source Files , which usually have the  "filename.c"  or "filename .C "
respectively contain the implementations (definitions) of the functions etc declared in the header files. i.e the actual lines of C/ C++   code.

What happens when the Compiler is run?

To produce an executable program from the source code requires a 3-stage process. The first 2 steps are carried out independently for each .c or .cpp

file.

   * (1) Pre-process: First the C pre-processor (cpp) is run on each source (.cpp) file. This interprets pre-processor directives, which are indicated by the

# symbol. The most important of these is the #include statement. The statement

#include "sample.h"
  places the contents of the local header file "sample.h" in the body of the code. Whilst

#include <stdio.h>
  places the contents of the standard header file stdio.h in the code.

   * (2) Compile: The expanded source code file resulting from pre-processing (translation unit) is compiled to produce an object file (filename .obj or .o)

containing the raw machine instructions.

   * (3) Linking: The Linker (Loader) combines one or more object files together with standard libraries (which contains collection of object files) to produce

an executable (filename .exe or no suffix). This is the program that one actually runs.
There are many compilers available in the market today  , Each of them use  their own methodologies to convert a C program into a executable format !  ,
 Of course the compiler performs different task on a  C program , ( Analyzing  , Intermediate code generation , and Creation of EXE File )  analyzer will preform a complete analysis of the program ( Which include token phrasing and so on )

After analysis phase the compiler will in turn generate a report on the code ( Used to called an intermediate code )   May be that code will not be in standard format  , only compiler can understand that code,

Using the intermediate code the compiler will in turn generate A machine specific instruction set ( For eg you computer using intel 8086 processor then your compiler will generate instruction set that is compatible with the instruction set of the 8086 processor ) if this instructions are supplied to the processor  , it can clearly understand the instructions and will perform the task well ! this file is called an EXE file !

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.