SlideShare ist ein Scribd-Unternehmen logo
1 von 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/

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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)
 

Kürzlich hochgeladen

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
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
 
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
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
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
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
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.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
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
 
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
 

Kürzlich hochgeladen (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
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
 
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 ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
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-...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
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...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
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...
 

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)