Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

The Process of Compilation

(In Computer Programming)

Abstract---This paper represents a First Year B.Tech


practice research paper as per the instructions given by /* helloworld.c */
our Professor Ms. Vidya T. In this paper, I would study
the general process of compilation used for creating To compile and run this C program every part of the system
various software from their source code written in their has to perform in concert. In order to compile above C
respective programming languages. Here I will use The program in Linux, we will start right from the creation of the
C Programming language for explaining various program. The 'Hello World!' program starts its life as a
processes involved in compiling and the compiler used source file which is created with help of a text editor and
will be The GCC. saved as helloworld.c. The helloworld.c program code is
stored in a file as a sequence of bytes. Each byte has a value
I. INTRODUCTION corresponding to some character. The first byte has the value
Compilation is a process used for converting a computer 35 that corresponds to the character '#', for example.
programmable code written in suitable programming Likewise, the second byte has the integer value 105, which
language (High or Low level) into an executable (in corresponds to the character 'i', and so on. The idea
Windows i.e. .exe file) or a binary (in Linux) which is illustrates that all information in a system is represented as a
actually the machine code (i.e. code written in 0’s and 1’s). bunch of bits.
This is necessary since computer can understand only the To compile and run the C program helloworld.c, all C
machine code. Programming languages were created since it statements must be translated individually into a sequence of
was very inefficient to write software in 0’s and 1’s. And instructions that a machine can understand. These
compilers did the job of translators which translated this instructions are then packaged in a form called executable
languages into the machine code. Compilers are actually one object program. There are other programs which perform
of the most important tools in a programmer’s life, this task to get the program running. On a UNIX/Linux
otherwise every programmer would have to burn in the fire system, the translation from source code to object code
of just 0’s and 1’s. (executable) is performed by a compiler driver. Here we will
Basically, the compilation process is broken down into 4 compile C program by gcc.
steps: pre-processing, compilation, assembly, and linking. The following command (provided that gcc is installed on
your Linux box) compiles C program helloworld.c and
II. WHAT IS COMPILING? creates an executable file called helloworld. [root@host ~]#
The following figure shows the process of compilation: gcc helloworld.c -o helloworld
While compiling helloworld.c the gcc compiler reads the
source file helloworld.cand translates it into an
executable helloworld. The compilation is performed in four
sequential phases by the compilation system (a collection of
four programs -preprocessor, compiler, assembler,
and linker).
Now, let's perform all four steps to compile and run C
program one by one.

1. Preprocessing
During compilation of a C program the compilation is
started off with preprocessing the directives (e.g., #include
Fig 1.0 Process of compilation. [1] and #define). The preprocessor (cpp - c preprocessor) is a
separate program in reality, but it is invoked automatically
To understand this process in detail, let us take an example by the compiler. For example, the #include
of a simple program written in C. <stdio.h> command in line 1 ofhelloworld.c tells the
Kernighan and Ritchie (K & R) in their classic book on C preprocessor to read the contents of the system header
programming language acquaint readers to C language by filestdio.h and insert it directly into the program text. The
compiling and executing "Hello World!" C program as result is another file typically with the .i suffix. In practice,
follows. [2] the preprocessed file is not saved to disk unless the -save-
temps option is used.
#include <stdio.h> This is the first stage of compilation process where
int main() preprocessor directives (macros and header files are most
{ common) are expanded. To perform this step gcc executes
printf("hello, world!\n"); the following command internally.
} [root@host ~]# cpp helloworld.c > helloworld.i
The result is a file helloworld.i that contains the source code
with all macros expanded. If you execute the above 4. Linking
command in isolation then the filehelloworld.i will be saved This is the final stage in compilation of "Hello World!"
to disk and you can see its content by vi or any other editor program. This phase links object files to produce final
you have on your Linux box. executable file. An executable file requires many external
resources (system functions, C run-time libraries etc.).
2. Compilation Regarding our "Hello World!" program you have noticed
In this phase compilation proper takes place. The compiler that it calls the printf function to print the 'Hello World!'
(ccl) translateshelloworld.i into helloworld.s. message on console. This function is contained in a separate
File helloworld.s contains assembly code. You can explicitly pre compiled object file printf.o, which must somehow be
tell gcc to translate helloworld.i to helloworld.s by executing merged with our helloworld.o file. The linker (ld) performs
the following command. this task for you. Eventually, the resulting file helloworld is
[root@host ~]# gcc -S helloworld.i produced, which is an executable. This is now ready to be
The command line option -S tells the compiler to convert the loaded into memory and executed by the system.
preprocessed code to assembly language without creating an During the whole compilation process there are other files
object file. After having created helloworld.s you can see the also in role along with the source code file.
content of this file. While looking at assembly code you may
note that the assembly code contains a call to the external III. SUMMARY
function printf. In this tutorial we explained compilation and execution
process steps and stages of C program in Linux using GCC.
3. Assembly Various phases during compilation and execution process of
Here, the assembler (as) translates helloworld.s into a C program take place, such as, pre-processing,
machine language instructions, and generates an object compilation, assembly, and linking.
file helloworld.o. You can invoke the assembler at your own
by executing the following command. REFERENCES
[root@host ~]# as helloworld.s -o helloworld.o [1]http://blog.techveda.org/understanding-a-c-programs-
The above command will generate helloworld.o as it is compilation-process/
specified with -o option. And, the resulting file contains the [2]http://cs-fundamentals.com/c-programming/how-to-
machine instructions for the classic "Hello World!" compile-c-program-using-gcc.php
program, with an undefined reference to printf.

You might also like