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 m

Comments

Popular posts from this blog

Operators And Separators In C Language Operators in C Language:

Difference between asterisk and ampersand operators in c