4 steps successful Build (Big Figure Behind Compilation)

 
We all wanna be a coder (that’s why you're going through this hefty blog, LOL).So, Let us sort out the basic understanding behind Compilation and how does it work.
        
                       
              What is meant by Compilation?
        
Journey of A Source Code To Execution Code



The process of translating source code written in high level to low level machine code is called as Compilation. The compilation is done by a special software known as compiler. The compiler checks source code for any syntactical or structural errors and generates object code with extension .obj (in Windows) or .o(in Linux) if source code is error free.

   How does a compiler understands what we want to do with a piece of code?
The Answer Is The Header Itself.......
               

                "THE 4 STEPS TO SUCCESSFUL BUILD" (Compilation Steps)

      
Compilation Steps


  Let’s visualize compilation Process hierarchy which can unlock debugging efficiency.  

   Statutory Caution : This Post Can Make You A Good Coder. Ponder on your own risk
    
Compiler Process can be termed as :-  Edit – Compile – Link –Execute Process. It's a multi-stage process that can be split up into 4 stages namely -
                             Preprocessing - Compilation - Assembly -Linking  (Shown Below).
4 Steps to successful compilation

   Let's Understand them hierarchically

                                                                 1. Preprocessing

The very first stage of compilation is known as preprocessing. Interpretation of lines preceding with # are  done using preprocessor commands. 
               --   These preprocessor commands contains simple macro language which  enables functionality of inline files, define macros and conditionally omit codes as well.

Note:-  All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column.

    Important Preprocessor Commands are mentioned below:
              
              
Preprocessor Directives
Description

1.       Macro(#define)
Define a macro which can be used as a constant throughout the source code and can be of a basic data types.
Syntax:-   #define VAR_NAME value
Ex:-
# define N 10 – Here, a constant variable  N with constant Value 10 is created using # define. Remember a macro should be in “ALL CAPS”





2.      Header files(#inlude)
Includes another C file(source code)  into the current file at the location of the   #include (replacing the header flies name with its source code) statement prior to compiling the source code.
Syntax:- #include < Header_File_Name>  OR #include “Header_File_Name”
Here, <> asks compiler to search for predefined header files and not user defined header files. Whereas “ “ asks compiler to search for a user defined header files.
Also <> searches header files in standard list of system directories whereas
 “ ” searches the header file in the current directory containing the current file.





3.Conditional Compilation
(#if,#ifdef,#endif,#else,#ifndef)
#if - Conditional expression which can be used to include source code for compilation.
#ifdef Allows the inclusion of source code if the provided macro identifier has been defined. Equivalent to #if defined(identifier).
#ifndef  Allows the inclusion of source code if the provided macro identifier has not been defined.
#else Allows the inclusion of source code if the preceeding #if, #ifdef, or #ifndef directive expression evaluates to false.
#endif Signals the end of a #if, #ifdef or #ifndef condition .
#elif -  Provides an alternate inclusion of source code when used with the #if, #ifdef, or #ifndef directives and the #elif condition evaluates to true.
Remember:-
These #include statements are conditional and are used in a code to exclude or include specific commands in source code before compilations with respect to conditions provided by us.

3.      Error Producing and reporting
( #warning, #error )
#warning  - Report a warning message and continue                    
                       preprocessing.
#error - Report error and stop preprocessing.

4.      Other Directive
(#undef , #pragma )
#undef -  Clear a macro which was previously defined.
#pragma  A method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself.  It’s for compiler directives that are machine-specific or operating-system-specific, i.e. it tells the compiler to do something, set some option, take some action, override some default, etc. that may or may not apply to all machines and operating systems.

              






            
                                                       





















                                                            














                                                                 2. Compilation
The second stage of compilation is named compilation. In this stage, the preprocessed is translated to Assembly Language specific to the target processor built.
        It accepts temporary pre-processed .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.
Note:-
    
This step allows  C code to have inline assembly instructions and for different Assemblers.
This will create a file named file_name.s containing the generated assembly instructions. 
It takes the output of the preprocessor, and the source code, and generates assembler source code.
  

                                                             3. Assembly

An assembler is used to translate the assembly instructions to machine code or object code
Assembler accepts the compiled source code (compilation.s) and translates to low level machine code. After successful assembling it generates  (in Linux) or  (in Windows) file known as object file. 
   The processed file is in low level language and cannot be viewed using Editors.

                                                                       
                                                                       4. Linking 

The linker comes in action and performs the final task of compilation process. 
The linker will arrange the pieces of object code so that functions in some pieces can successfully call functions in other pieces. It will also add pieces containing the instructions for library functions used by the program.
It accepts the intermediate file  generated by the assembler and 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).



This is what happens in a click by a software known as gcc. 

                                           gcc  : - 

GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom.
GCC is portable and run in many operating platforms. GCC (and GNU Toolchain) is currently available on all Unixes. They are also ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is also a cross-compiler, for producing executables on different platform.

Hope You'll be able to understand the concept behind COMPILATION. 
Ping me for more..
Regards,
Xavier.

Comments

Popular posts from this blog

Introduction To Pointers

Keywords (Extension to Fragments Of C)