C++ Compilation Model C++ is a compiled language

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CSE202: Lecture 1The Ohio State University1 Introduction to C++
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Libraries Making Functions Globally Reusable (§ 6.4) 1.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
1 C++ Syntax and Semantics, and the Program Development Process.
Libraries 1 Making Functions Globally Reusable (§ 4.6)
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Libraries Making Functions Reuseable. Review Last time, we wrote a program to compute the area and circumference of an ellipse. a a bb.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Libraries 1 Making Functions Globally Reusable (§ 6.4)
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Chapter 1: Introduction to Computers and Programming
C++ First Steps.
Chapter 1: An Overview of Computers and Programming Languages
Chapter Topics The Basics of a C++ Program Data Types
What Is? function predefined, programmer-defined
Topic Pre-processor cout To output a message.
Executable program (p1)
Chapter 1: Introduction to computers and C++ Programming
Separate Compilation and Namespaces
Chapter 1: An Overview of Computers and Programming Languages
Basic Elements of C++.
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Basic Elements of C++ Chapter 2.
Chapter 1: Introduction to Computers and Programming
Separate Compilation and Namespaces
Separate Compilation.
Name: Rubaisha Rajpoot
Chapter 1: An Overview of Computers and Programming Languages
C Preprocessor(CPP).
Comments, Prototypes, Headers & Multiple Source Files
Code Organization CSCE 121 J. Michael Moore.
Chapter 1: An Overview of Computers and Programming Languages
Namespaces How Shall I Name Thee?.
Programming Introduction to C++.
Separate Compilation.
What Is? function predefined, programmer-defined
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
SPL – PS1 Introduction to C++.
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

C++ Compilation Model C++ is a compiled language Compiler converts the C++ program directly to machine code Once the program is compiled, the resulting executable can be run any number of times, even if the source code is nowhere to be found Compilation steps 1. Preprocessing, code segments are spliced and inserted 2. Compilation, code is converted to object code 3. Linking, compiled code is joined together into a final executable

C++ Compilation Model Preprocessing (Preprocessor) 1. Scans over the C++ source code 2. Applies various transformations to it 3. #include directives are resolved to make various libraries available, #define-d constants are placed by their values Compilation (Compiler) 1. Source file is read in by the compiler, optimized, and transformed into an object file 2. Machine specific, contain machine code which executes the instructions specified in the C++ file, with some extra information 3. Compiler reports syntax errors Linking (Linker) 1. Gathers all object files necessary to build the final executable 2. Bundles them together with OS-specific information 3. Produces an executable file 4. linker may report some final errors that prevent it from generating a working C++ program

C++ Compilation Model #include <iostream> using namespace std; int Factorial(int n); // Prototype int main() { cout << Factorial(10) << endl; return 0; } This is a link error since we cannot find the definition of the function.

C++ Compilation Model Main phases of compilation

C++ Compilation Model Since the compiling and linking are separate, we can split the C++ program into multiple files Advantages 1. Organized 2. Secure, only provide the compiled file without source files, binary files 3. Portable, put code in its own file, we can move the files from one project to another

Multiple Files Main file 1. Like first program without functions 2. Header file 3. Namespace 4. main function Header file 1. Function prototypes, global constants, struct definitions, class definitions 2. Named by the project’s purpose 3. Have a .h extension 4. .h tells compiler that it is a header file and NOT to be compiled 5. Documenting file Implementation file 1. Contain definitions of the functions that are prototyped in the header 2. .cpp files will be compiled

Preprocessor Directives At the top #ifndef MYFILE_H #define MYFILE_H At the bottom #endif Examples (treeFarm.h) #ifndef TREEFARM_H // treeFarm.h in uppercase follows the convention #define TREEFARM_H const float OAK_DENSITY = 4.25; // pounds per square foot etc #endif #ifndef MyStruct_Included #define MyStruct_Included struct MyStruct { int x; double y; char z; };