2. LINKERCOMPILERPREPROCESSO
R
The preprocessor
handles
statements or
lines of code that
begin with the "#"
character, which
are called
"preprocessor
directives."
The compiler
translates C++
source code into
the machine code
that a given
computer
"understands" and
can execute.
The linker takes
the object files
created by the
compiler and links
them together,
along with library
code and a
runtime file, to
form a complete,
executable
program that is
stored in a single
file.
Are there any
Preprocessor
Directives in
Source program
TranslatetoObject
Code
Processedsource
code
Source
Code
Executable
Code
Ye
s
No
C/C++ OPERATIONS
3. C++ SOURCE
Main.h
#pragma once
#include <iostream>
using namespace std;
#define name “Basic
Math”
void PrintName();
Main.cpp
#include “Main.h”
#include “Math.h”
void PrintName(){
cout << name <<
endl;
}
int main()
{
PrintName();
int val = Add( 10,
12);
cout << val << endl;
return0;
}
Math.h
#pragma once
int Add(int a, int b);
Math.cpp
#include “Math.h”
int Add(int a, int b)
{
return a + b;
}
4. C++ PRE-PROCESSING
The compilation task contains a initial processing phase of the source file. The initial phase is called
preprocessing. The preprocessing is executed by a preprocessor invoked by the compiler.
Now, a preprocessor is simply a directive that starts with #. So, #define, #include, #if, #then, #else
and #line are some of the preprocessors with which the compiler interacts.
The preprocessor looks through the source code for all the lines which are starting with the # (hash)
key. These lines are called compiler directives.
One of the compiler’s directive is to include functions which are defined externally from our source
code. The preprocessor removes all the compiler directives from the source code but remembers
what additional files need to be included later in the process. At the end of the preprocessing a
temporary filed will be created, not visible to the user.
5. C++ PRE-PROCESSING
Main.temp
int main(){
PrintName();
int val = Add( 10,
12);
cout << val << endl;
return0;
}
void PrintName(){
cout << name <<
endl;
}
Math.tem
p
int Add(int a, int b);
int Add(int a, int b)
{
return a + b;
}
Main.h
#program once
#include <iostream>
using namespace std;
#define name “Basic
Math”
void PrintName();
Main.cpp
#include “Main.h”
#include “Math.h”
void PrintName(){
cout << name <<
endl;
}
int main()
{
PrintName();
int val = Add( 10,
12);
cout << val << endl;
return0;
}
Math.h
#program once
int Add(int a, int b);
Math.cpp
#include “Math.h”
int Add(int a, int b)
{
return a + b;
}
Source Code
Code after
preprocessing
6. C++ COMPILING
The compiler converts preprocessed file into an object file. The object file is also named machine
code and can be interpreted by the Central Processing Unit of the computer or microcontroller.
The object file is ready but it is missing some undefined references. These undefined references are
pieces of code which have to be retrieved form a different place. In our case the undefined
reference is the cout() function. We know from where to get the code for this function because it
was specified by the compiler directive (#include<iostream.h>).
7. C++ COMPILING
Main.o
name:0x1000
main:0x2000
PrintName:0x3000
cout << 0x1000 <<
endl
val:0x4000
Add(1010, 1100)
cout << 0x4000 <<
endl
return 0x0000
Math.o
Add:0x5000
a:0x6000
b:0x7000
--------------------
-----------
0x5000(0x6000,
0x7000)
Object Code(Demo)
Main.temp
int main(){
PrintName();
int val = Add( 10,
12);
cout << val << endl;
return0;
}
void PrintName(){
cout << name <<
endl;
}
Math.tem
p
int Add(int a, int b);
int Add(int a, int b)
{
return a + b;
}
Preprocessed Code
8. C++ LINKING
The linker takes the object files created by the compiler and links them together, along with library
code and a runtime file, to form a complete, executable program that is stored in a single file.
This means that we need to have the object files and the static library files for the external functions.
The static library files (*.lib) contain the definition of the external functions used in our source file. In
our particular case the static library file will contain the machine code of the cout() function.
At the end of the linking process we are going to have an executable file (e.g. *.exe for Windows
applications, *.hex for microcontrollers).
The name of the executable file depends on the hosting operating system: On a Windows computer,
the linker produces a file whose name ends with a ".exe" extension. On Linux, Unix, and OS X
computers, the linker produces a file named a.out by default but the user may specify a different name