SlideShare uma empresa Scribd logo
1 de 10
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/

Mais conteúdo relacionado

Mais procurados

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 reportRaushan 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 programming interview questions
C programming interview questionsC programming interview questions
C programming interview questionsadarshynl
 
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 ScenarioDurgesh Singh
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answersDeepak Singh
 
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. IntroductionRanel Padon
 
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#Sagar Pednekar
 

Mais procurados (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
 

Semelhante a How c/c++ works

Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN 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 itPushkarNiroula1
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
General structure of c++
General structure of c++General structure of c++
General structure of c++Ajay Chimmani
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework HelpC++ Homework Help
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 
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 LanguageProf Ansari
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
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
 
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
 

Semelhante a 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)
 

Último

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 

Último (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 

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)