SlideShare a Scribd company logo
How C/C++ Works
Ekhlasur Rahaman
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
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;
}
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.
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
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>).
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
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
LINKER
Main.exe/.out 0x2000
0x3000
0x110 0x1000 0x1000
0x1111 0x1121
0x5000(1010, 1100)
0x110 0x4000 0x1000
0x1111 0x1121
Executable
Code(Demo)
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)
REFERENCES
http://icarus.cs.weber.edu/~dab/cs1410/textbook/1.Basics/co
mpiler_op.html
https://www.freelancer.com/community/articles/how-c-works-
understanding-compilation
https://www.cs.odu.edu/~zeil/cs250PreTest/latest/Public/cppP
rogramStructure/index.html
https://x-engineer.org/graduate-engineering/programming-
languages/c/how-c-programming-works-from-editor-to-
executable/

More Related Content

What's hot

Programming In C++
Programming In C++ Programming In C++
Programming In C++
shammi mehra
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
Raushan Pandey
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Overview of c++
Overview of c++Overview of c++
Overview of c++geeeeeet
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
adarshynl
 
Object Oriented Programming With Real-World Scenario
Object Oriented Programming With Real-World ScenarioObject Oriented Programming With Real-World Scenario
Object Oriented Programming With Real-World Scenario
Durgesh Singh
 
Deep C
Deep CDeep C
Deep C
Olve Maudal
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answers
Deepak Singh
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
starlit electronics
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
Prof. Dr. K. Adisesha
 
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#
Sagar Pednekar
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
nTier Custom Solutions
 

What's hot (20)

Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Basics1
Basics1Basics1
Basics1
 
Overview of c++
Overview of c++Overview of c++
Overview of c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
 
C vs c++
C vs c++C vs c++
C vs c++
 
Object Oriented Programming With Real-World Scenario
Object Oriented Programming With Real-World ScenarioObject Oriented Programming With Real-World Scenario
Object Oriented Programming With Real-World Scenario
 
Deep C
Deep CDeep C
Deep C
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answers
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
C vs c++
C vs c++C vs c++
C vs c++
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
 
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 

Similar to How c/c++ works

basic program
basic programbasic program
basic program
NIDA HUSSAIN
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
REHAN IJAZ
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
PushkarNiroula1
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
SHRIRANG PINJARKAR
 
General structure of c++
General structure of c++General structure of c++
General structure of c++
Ajay Chimmani
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
farishah
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
bhargavi804095
 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming Language
Prof Ansari
 
C++ basics
C++ basicsC++ basics
C++ basics
AllsoftSolutions
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
Danielle780357
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
NEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
bhargavi804095
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
MohammedAlobaidy16
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
Prashant Sharma
 

Similar to How c/c++ works (20)

Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
basic program
basic programbasic program
basic program
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
 
General structure of c++
General structure of c++General structure of c++
General structure of c++
 
Built in function
Built in functionBuilt in function
Built in function
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming Language
 
C++ basics
C++ basicsC++ basics
C++ basics
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 

Recently uploaded

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 

Recently uploaded (20)

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 

How c/c++ works

  • 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
  • 9. LINKER Main.exe/.out 0x2000 0x3000 0x110 0x1000 0x1000 0x1111 0x1121 0x5000(1010, 1100) 0x110 0x4000 0x1000 0x1111 0x1121 Executable Code(Demo) 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)