Compilation Stages In C Programming - Ocean of Programming

Saturday 8 August 2020

Compilation Stages In C Programming

 

         In this article, I am going to explain the compilation stages in C before that we will see some other basics.


What is mean by compilation?

    The compilation is a process of converting the source code into an executable file. It can be done with the help of the compiler such as Code Block if you want to know how to download install code block click here check my previous article. The compiler will check for error and if the code is error-free then it will generate the executable file. The compiler converts the code into machine language or code that a computer's processor uses. The executable file type depends on the compiler. Some of the executable file types are ".exe", ".hex", ".elf", etc.


Before generating the executable the source code passes into 4 phases. 

      - Pre-processing

      - Compiler

      - Assembler  

      - Linker

 

 Now lets us understand each phase in detail. 

 

1. Pre-processing

    This is the first stage of the compilation. In this stage, the line starting with '#' is considered as a pre-processor command. The stage includes  

 

      - Macro definition and expansion

      - Conditional compilation 

      - Removal of Comments 

      - Expansion of the included files

 

    We can execute the command: gcc -E filename.c. It will generate file a filename.i  and results are printed on the standard console. 

 

 

2. Compilation

    In this stage, the expanded code is passed to the compiler and the compiler converts the code into assembly code. 


    We can execute the command: gcc -S filename.c. It will generate file a filename.s and it is human-readable. 

 


3. Assembly

    In this stage, the code is translated into machine code with the help of an assembler. It will create object files.

    We can execute the command: gcc -c filename.c. It will generate file a filename.o and the file is not human readable.


   

4. Linker
    This is the final stage of compilation. The main working of the linker is to combine program object files with library object files.
It will take object files that are generated by the assembler and add pre-compiled library files. The output of the linker is executable.

We can execute the command: gcc -o executablefile filename.c.

If you like this article then please don't forget to share with your friends and subscribe to my blog.

No comments:

Post a Comment