Lifecycle from .c to .out in Linux


Suppose we have written a C-Program as the following

#include<stdio.h>
#define pi 3.14
int main()
{
        printf("\n the value of pi is %d",pi);
        return 0;

}



Stage 1: Pre-Processing:
          In this stage all the things which start with a #(macros,includes) gets expanded.All the comments will also be removed in this stage(//, /* */)
In our example the contents of stdio.h will be included in our file and pi will be replaced with 3.14

O/P of Pre-processing stage is still a C file

Linux Command: gcc -E file.c


Step 2: Compiling
        In this stage c code will be  converted into assembly code.First the syntax checking will be done if the syntax is ok then each C instruction will be converted into its assembly instruction. The code will now have instructions such as push,movx etc

O/P of compiling stage is Assembly Code

Linux Command : gcc -S file.c



Step3: Assembly:

  The assembly code is given as input to the assembler and he generates an object code.At this stage the code is converted to machine code.But still the functions like printf() are still not replaced with their definition

O/P of Assembly Stage is Object Code
Linux Command : gcc -c file.c


Step4: Linking

     This is the final stage . Here the machine code is taken and all the function calls like printf() are linked with their definitions. Multiple object files also can be linked to generate a executable

O/p of Linking Stage: Executable
Linux command : gcc -o file file.c
       

Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto