This blog is under construction

Sunday, 22 September 2013

Steps involved in compiling a c program using gcc

Steps involved in converting the C source code to executable:
Now, we are going to see how to convert a C source code to executable and also about the steps involved in compiling a C program.  C program compilation involves 4 steps which are as follows.
  • Converting C source code to Preprocessed code
  • Converting preprocessed code to assembly code
  • Converting assembly code to object code
  • Converting object code to binary


Steps involved in compiling a C Program

Here, we have two c files main.c and add.c.

Below is the source code for main.c
 
 #define NUM1 10
 #define NUM2 20

  int main() {
        int a, b;
        a = NUM1;
        b = NUM2;
        add(a, b);
        return 0;
  }


Below is the source code for add.c
 
  #include <stdio.h>
  #define STRING "Result:"

  void add(int a, int b) {
        int c;
        c = a + b;
        printf("%s %d\n", STRING, c);
        return;
  }


As I mentioned earlier, compiling a C program involves 4 phases.  They are preprossesing, compilation, assembly and linking.  We don't need to go through all the above four steps one by one to get final executable for the given source code.  Because, compiler can do all the above operation implicitly and provide us the final binary or executable in one single command(see below).

gcc <file_name.c> -o <execuable_name>

Please note that the above command itself can get us the executable for the given source code.  Just to understand whole compilation process, we are going to go through all the steps involved in compilation one by one.

What is preprocessor?
It converts the C source code to preprocessed code or expanded c source code. In other words, its a program which runs on our C source code before compilation process.  And it provides facility to handle named constant, macros and file inclusion.  Preprocessor begins with the preprocessor directive hash symbol(#).  During preprocessing the following operation would take place.
  • macro substitution
  • File inclusion
  • Conditional compilation
We will see more about the above operations in-detail in the forth coming chapters.

How to convert C source code to preprocessed code?
We can use -E option to perform preprocessing alone.  The below command converts the C source code to preprocessed code.  The -o option is used to redirect the preprocessed code to the given file.

gcc -E main.c -o main.i -> redirects the preprocessed code to the file main.i
gcc -E add.c -o add.i    -> redirects the preprocessed code to the file add.i

  jp@jp-VirtualBox:~/$ gcc -E main.c -o main.i
  jp@jp-VirtualBox:~/$ gcc -E add.c -o add.i
  jp@jp-VirtualBox:~/$ ls
  add.c  add.i  main.c  main.i

Below is the preprocessed code(main.i) of main.c
 
# 1 "main.c"
 # 1 "<built-in>"
 # 1 "<command-line>"
 # 1 "main.c"

 int main() {
    int a, b;
     a = 10;
     b = 20;
     add(a, b);
     return 0;
 }



Below is the preprocessed code(add.i) of add.c
 
 # 936 "/usr/include/stdio.h" 3 4
 # 2 "add.c" 2

 void add(int a, int b) {
     int c;
     c = a + b;
     printf("%s %d\n", "Result:", c);
     return;
 }


Note:  In the above preprocessed source codes, you could find that the macros are replaced by values.

What is a compiler?
Compiler converts the preprocessed(or C) source code to assembly language code.

How to convert preprocessed source code to assembly language code?
The -S option tells the compiler to generate assembly language code alone.  The below command converts the preprocessed source code to assembly language code.   The -o option is used to redirect the assembly language code to the given file.

gcc -S main.i -o main.s -> redirects the assembly code for main.i to the file main.s
gcc -S add.i -o add.s    -> redirects the assembly code for add.i to the file add.s

  jp@jp-VirtualBox:~/$ gcc -S main.i -o main.s
  jp@jp-VirtualBox:~/$ gcc -S add.i -o add.s
  jp@jp-VirtualBox:~/$ ls
  add.c  add.i  add.s  main.c  main.i  main.s


Below is the assembly code(main.s) for main.i
 
   .file   "main.c"
        .text
  .globl main
        .type   main, @function
  main:
        pushl   %ebp
        movl    %esp, %ebp
        andl    $-16, %esp
        subl    $32, %esp
        movl    $10, 28(%esp)
        movl    $20, 24(%esp)
        movl    24(%esp), %eax
        movl    %eax, 4(%esp)
        movl    28(%esp), %eax
        movl    %eax, (%esp)
        call    add
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .ident  "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
        .section        .note.GNU-stack,"",@progbits



Below is the assembly code(add.s) for add.i
 
        .file   "add.c"
        .section        .rodata
  .LC0:
        .string "%s %d\n"
  .LC1:
        .string "Result:"
        .text
  .globl add
        .type   add, @function
  add:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $40, %esp
        movl    12(%ebp), %eax
        movl    8(%ebp), %edx
        leal    (%edx,%eax), %eax
        movl    %eax, -12(%ebp)
        movl    $.LC0, %eax
        movl    -12(%ebp), %edx
        movl    %edx, 8(%esp)
        movl    $.LC1, 4(%esp)
        movl    %eax, (%esp)
        call    printf
        leave
        ret
        .size   add, .-add
        .ident  "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
        .section        .note.GNU-stack,"",@progbits



What is assembler?
Assembler converts the assembly language code to object code.

How to convert assembly language code to object code?
The -c option tells the compiler to generate object code(machine codes).  The below command converts the assembly language code to object codes.  The -o option is used to redirect the object code to the given file.

gcc -c main.s -o main.o -> redirects the object code for main.s to the file main.o
gcc -c add.s -o add.o    -> redirects the object code for add.s to the file add.o

  jp@jp-VirtualBox:~/$ gcc -c add.s -o add.o
  jp@jp-VirtualBox:~/$ ls
  add.c  add.i  add.o  add.s  main.c  main.i  main.o  main.s


What is linker?
Linker links one or more object files and generates a single executable.

How to convert objects codes to an executable?
The below command links the given object files and generates an executable file.  The -o option is used to mention the name of the executable file.

gcc main.o add.o -o output -> output is the executable file

  jp@jp-VirtualBox:~/$ gcc main.o add.o -o output
  jp@jp-VirtualBox:~/$ ls
  add.c  add.i  add.o  add.s  main.c  main.i  main.o  main.s  output
  jp@jp-VirtualBox:~/$ ./output
  Result: 30

./output - "./" indicates current directory.  And "output" is the name of the executable.

So, our program prints the sum value of two numbers 10 and 20.


No comments:

Post a Comment