Skip to main content

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.


Comments

Popular posts from this blog

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Save image in Microsoft Paint without white background

Here, we are going to see how to save image in mspaint without white background 1. Create your own drawing or painting 2. Crop the needed portion of your drawing or painting or image. Here, I am going to crop the porting inside the red box. Now, we have cropped the portion that needs to be saved as an image. 5. Clear the screen and paste the image which we cropped earlier. 6. Decrease the screen size by 50% 7. Now, adjust your screen to fit to your image size. 8. Save the image to desired file format(jpeg, png, bmp etc) 9. Go to the saved location and open the file.  Below is the output image of mine Hope you liked this article!!  For any suggestions, please comment below.