You are on page 1of 2

COMPILATION STAGES

COMPILATION:

It is a process of translating the code of a higher-level language to the code


of a lower-level language (typically, assembler or even machine code) production
files.

THE STAGES OF COMPILING:

There are four modules in the compilation stages. They are

1) Pre-Processor
2) Translator
3) Assembler
4) Linker

PRE-PROCESSOR

The standard first step in processing the source files is running them through
the special text processing program called a preprocessor, which performs one or
more of the following actions:

The pre-processor including header files into the source code files, as
specified by the keyword #include.

Replacement of Macros (i.e., converts the macro definitions into code at


the variety of locations in which the macros are invoked.)
Conditional compilation (i.e., Conditionally includes or excludes certain
parts of the code, based on the position of #if, #elif, and #endif directives)
Removing the comments (if any) in your program.
The output of the preprocessor is the C/C++ code in its final shape. The
resultant intermediate file extension is .i contains the expanded source
code.

TRANSLATOR:

Its input is .i extension file contains pure c code.


The compiler translates the high-level instructions in the .i file into low-
level Assembly language instructions
The generated file containing the translation is in text format and
typically has a .s file extension

ASSEMBLER:

The input file from translator (.s) is not suitable for execution. It is a
text file carrying the human-readable mnemonics of assembler
instructions.
It can be used by the developer to get a better insight into the details of
the inner workings of the compilation process.
Now the assembler takes the .s file as an input and produce the .o file as
an output. This .o file is called as object file.
At this stage only the existing code is converted into machine language,
the function calls like printf ()/scanf () are not resolved.

LINKER:

The last stage of compilation is the linking of object files to create an


executable file (a.out).
The input to the linking process is the collection of object files created
by the assembler.
In order to create the executable file (a.out) the linker adds some extra
code to our program.
For ex: we use the printf/scanf function. Our current object file has no
idea about that function. So if we want to use that function means, we
need linkers help.
The linker links our program with the precompiled libraries which
contains pre-define function definitions (printf/scanf) then, it link
together and creates the executable file.