0% found this document useful (0 votes)
39 views4 pages

What Is Meant by Compilation?: High Level Lo W Level Compiler

The compilation process translates source code into machine code in four stages: preprocessing, compilation, assembly, and linking. Preprocessing removes comments and includes header files. Compilation checks for errors and generates assembly code. Assembly converts assembly to machine code, producing an object file. Linking combines object files and library code, resolving function calls to produce an executable file.

Uploaded by

nesrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

What Is Meant by Compilation?: High Level Lo W Level Compiler

The compilation process translates source code into machine code in four stages: preprocessing, compilation, assembly, and linking. Preprocessing removes comments and includes header files. Compilation checks for errors and generates assembly code. Assembly converts assembly to machine code, producing an object file. Linking combines object files and library code, resolving function calls to produce an executable file.

Uploaded by

nesrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What is meant by Compilation?

The process of translating source code written in high level to low level machine code is called as
Compilation. The compilation is done by a special software known as compiler. The compiler checks
source code for any syntactical or structural errors and generates object code with extension .obj (in
Windows) or ,o (in Linux) if source code is error free.

The C compilation
The entire C compilation is broken to four stages.
1. Pre-processing
2. Compilation
3. Assembling 
4. Linking
Write the c program below
#include <stdio.h>
int main()
{
printf("Learn at Codeforwin!");
return 0;
}

To compile the above program open command prompt and hit below command.

gcc -save-temps compilation.c -o compilation

The -save-temps option will preserve and save all temporary files created during the C compilation.
It will generate four files in the same directory namely.
- compilation.i (Generated by pre-processor)
- compilation.s (Generated by compiler)
- compilation.o (Generated by assembler)
- compilation (On Linux Generated by linker) or (compilation.exe On Windows)

Pre-processing of source file


The C compilation begins with pre-processing of source file. Pre-processor is a small software that
accepts C source file and performs below tasks. The preprocessor (cpp - c preprocessor) is a separate
program in reality, but it is invoked automatically by the compiler.
• Remove comments from the source code.
• Macro expansion.
• Expansion of included header files.
After pre-processing it generates a temporary file with .i extension. Since, it inserts contents of
header files to our source code file. Pre-processor generated file is larger than the original source
file.
You can notice that the statement #include <stdio.h> is replaced by its contents. Comment before
the #include line is also trimmed.
In practice, the preprocessed file is not saved to disk unless the -save-temps option is used.

Compilation of pre-processed file


In next phase of C compilation the compiler comes in action. It accepts temporary pre-processed
<file-name>.i file generated by the pre-processor and performs following tasks.
• Check C program for syntax errors.
• Translate the file into intermediate code i.e. in assembly language.
• Optionally optimize the translated code for better performance.
After compiling it generates an intermediate code in assembly language as <file-name.s> file. It is
assembly version of our source code.
Assembling of compiled source code
Moving on to the next phase of compilation. Assembler accepts the compiled source code
(compilation.s) and translates to low level machine code. After successful assembling it generates
<file-name.o> (in Linux) or <file-name.obj> (in Windows) file known as object file.
This file is encoded in low level machine language and cannot be viewed using text editors.

Linking of object files


Finally, the linker comes in action and performs the final task of compilation process. It accepts the
intermediate file <file-name.o> generated by the assembler. It links all the function calls with their
original definition. Which means the function printf() gets linked to its original definition.
le compilateur agrège chaque fichier .o avec les éventuels fichiers binaires des librairies
qui sont utilisées (fichiers .a et .so sous linux, fichiers .dll et .lib sous windows).

- Une librairie dynamique (.dll et .so) n'est pas recopiée dans l'exécutable final (ce qui
signifie que le programme est plus petit et bénéficiera des mises à jour de ladite librairie).
En contrepartie, la librairie doit être présente sur le système sur lequel tourne le
programme.

- Une librairie statique (.a) est recopiée dans l'exécutable final ce qui fait que celui-ci est
complètement indépendant des librairies installées du le système sur lequel il sera
recopié. En contrepartie, l'exécutable est plus gros, il ne bénéficie pas des mises à jour de
cette librairie etc...

Le linker vérifie en particulier que chaque fonction appelée dans le programme n'est pas
seulement déclarée (ceci est fait lors de la compilation) mais aussi implémentée (chose
qu'il n'avait pas vérifié à ce stade). Il vérifie aussi qu'une fonction n'est pas implémentée
dans plusieurs fichiers .o.

Cette phase, appelée aussi édition de lien, constitue la phase finale pour obtenir un
exécutable (noté .exe sous windows, en général pas d'extension sous linux).

Huuhh, these all happens in a blink by a software known as gcc.


Lib vs Dll librairie ( .a & .so in linux)
When developing software, we are often asked whether we want to use LIB or DLLs in containing
functions for the application.

LIB is a static library where functions and procedures can be placed and called as the application is
being compiled.

A DLL or Dynamic Link Library does the same function but is dynamic in a sense that the application
can call these libraries during run-time and not during the compilation.

This presents a few significant advantages compared to using LIB :

For starters, you would have a single file that is significantly bigger as it contains all of the code
while you would have multiple smaller files when using DLL. Compiling your functions and
procedures would also allow you more reusability as once you are happy with the functions on the
DLL because you can keep it as is with each version of the application and not have to mess with it.
You can also use the same DLL if you want to create another application that uses the same
functions and procedures. You can directly link to the DLL rather than copy the code from the
source as you would need to do with LIB.

A problem with DLL is when you change the content of the DLL. This can lead to versioning
problems where an application uses the incorrect version of the DLL causing problems. You need to
keep track of your DLLs in order to avoid these problems. You would not have this problem with LIB
as you would only get one large file.

When developing the software and choosing DLL, you would still have a LIB file in your project. But
unlike when using LIB, this file does not contain the code of the functions and procedures but only
stubs that the program needs to call the procedures from the DLL’s.

Summary:
1. A DLL is a library that contains functions that can be called by applications at run-time while LIB is
a static library whose code needs to be called during the compilation
2. Using LIB would result in a single file that is considerable bigger while you end up with multiple
smaller files with DLL’s
3. DLL’s are more reusable than LIBs when writing new versions or totally new applications
4. DLL files can be used by other applications while LIB files cannot
5. DLL’s are prone to versioning problems while LIB is not
6.You would still have a LIB file when developing software with DLLs but it only contains stubs

You might also like