The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.

Slides:



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

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
CSE 303 Lecture 16 Multi-file (larger) programs
The Assembly Language Level
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Object-Oriented Programming in C++
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Lecture #5 Introduction to C++
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Basic Program Construction
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
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.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
C++ Functions A bit of review (things we’ve covered so far)
Bill Tucker Austin Community College COSC 1315
What Is? function predefined, programmer-defined
Computer Terms Review from what language did C++ originate?
Chapter 13 - The Preprocessor
Objectives Identify the built-in data types in C++
Separate Compilation and Namespaces
Structures putting data together.
Programming Fundamentals (750113) Ch1. Problem Solving
C Preprocessor(CPP).
Separating Definition & Implementation
Introduction to Classes and Objects
C++ Compilation Model C++ is a compiled language
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
C Preprocessor Seema Chandak.
A First Program.
C Programming Language
What Is? function predefined, programmer-defined
Conditional Compilation
Presentation transcript:

The Structure of a C++ Program

Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into Files 5. Project Directories & Libraries

1. Separate Compilation C++ programs can range from a handful of statements to hundreds of thousands C++ programs can range from a handful of statements to hundreds of thousands May be written by one person or by a team May be written by one person or by a team

Single File Programs OK for small programs (CS150) OK for small programs (CS150) But with large programs: But with large programs: –compilation would take minutes, hours, maybe days  might break compiler –Team members would interfere with one another’s work.  “Are you still editing that file? You’ve had it all afternoon.”  “What do you mean you’re saving changes to the file? I’ve been editing it for the last 45 minutes!”

Multiple File C++ Programs Team members can work in parallel on separate files Team members can work in parallel on separate files Files are compiled separately Files are compiled separately –each individual compilation is fast Separately compiled code is linked to produce the executable Separately compiled code is linked to produce the executable –linking is much faster than compilation

Separate Compilation Each file of source code Each file of source code –programming language text is compiled to produce a file of object code is compiled to produce a file of object code –binary code, almost executable –exact addresses of variables and functions not known, represented by symbols All object code files linked to produce the executable All object code files linked to produce the executable –Linking mainly consists of replacing symbols by real addresses.

Large Projects On large projects with hundreds to thousands of files Typically only a few files are changed on any one day Typically only a few files are changed on any one day Often only the changed files need to be recompiled Often only the changed files need to be recompiled Then link the changed and unchanged object code Then link the changed and unchanged object code

2. The # Preprocessor The preprocessor runs before the compiler proper The preprocessor runs before the compiler proper –modifies the source code –processes preprocessor instructions  lines beginning with # –strips out comments

Processor Instructions #include #include –insert a file #define #define –define a macro #ifdef, #ifndef, #endif #ifdef, #ifndef, #endif –check to see if a macro has been defined

#include Inserts a file or header into the current source code Inserts a file or header into the current source code Two versions Two versions –#insert –#insert  inserts a system header file from a location defined when the compiler was installed –#insert “fileName”  inserts a file from the current directory

#include Example Suppose we have three files: A.h, B.h, C.cpp Suppose we have three files: A.h, B.h, C.cppA.hB.hC.cppA.hB.hC.cpp We ask the compiler to only run the preprocessor and save the result: We ask the compiler to only run the preprocessor and save the result: g++ -E C.cpp > C.i The result is file C.i The result is file C.iC.i –Note the presence of content from all three files  includes markers telling where the content came from

#include Example 2 In real programs, most of the code actually seen by the compiler may come from #include’s In real programs, most of the code actually seen by the compiler may come from #include’s –helloWorld.cpp helloWorld.cpp –helloWorld.i helloWorld.i

#define Used to define macros (symbols that the preprocessor will later substitute for) Used to define macros (symbols that the preprocessor will later substitute for) –Sometimes used to supply constants #define VersionNumber "1.0Beta1" int main() { cout << "Running version “ cout << "Running version “ << VersionNumber << VersionNumber << endl; << endl; Much more elaborate macros are possible, including ones with parameters Much more elaborate macros are possible, including ones with parameters

#ifdef, #ifndef, #endif Used to select code based upon whether a macro has been defined: Used to select code based upon whether a macro has been defined: #ifdef __GNUG__ /* Compiler is gcc/g++ */ /* Compiler is gcc/g++ */#endif #ifdef _MSC_VER /* Compiler is Microsoft Visual C++ */ /* Compiler is Microsoft Visual C++ */#endif

#if, #define, and #include All of these macros are used to reduce the amount of code seen by the actual compiler All of these macros are used to reduce the amount of code seen by the actual compiler Suppose we have three files: A2.h, B2.h, C2.cpp Suppose we have three files: A2.h, B2.h, C2.cppA2.hB2.hC2.cppA2.hB2.hC2.cpp We ask the compiler to only run the preprocessor and save the result: We ask the compiler to only run the preprocessor and save the result: g++ -E C2.cpp > C2.i The result is file C2.i The result is file C2.iC2.i –Note that the code from A2.h is included only once –Imagine now, if that were iostream instead of A2.h

3. Declarations and Definitions Some of the most common error messages are Some of the most common error messages are –… is undeclared –… is undefined –… is defined multiple times Fixing these requires that you understand the difference between declarations and definitions Fixing these requires that you understand the difference between declarations and definitions –and how they relate to the program structure

Declarations A declaration in C++ A declaration in C++ –introduces a name for something –tells what “kind” of thing it is –gives programmers enough information to use it

Definitions A definition in C++ A definition in C++ –introduces or repeats a name for something –tells what “kind” of thing it is –tells what value it has and/or how it works –gives the compiler enough information to generate this and assign it an address

General Rules for Decls & Defs All definitions are also declarations. All definitions are also declarations. –But not vice-versa A name must be declared before you can write any code that uses it. A name must be declared before you can write any code that uses it. A name can be declared any number of times, as long as the declarations are identical. A name can be declared any number of times, as long as the declarations are identical. A name must be defined exactly once, somewhere within all the separately compiled files making up a program. A name must be defined exactly once, somewhere within all the separately compiled files making up a program.

D&D: Variables These are definitions of variables: These are definitions of variables: int x; string s = “abc”; MyFavoriteDataType mfdt (0); These are declarations: These are declarations: extern int x; extern string s; extern MyFavoriteDataType mfdt;

D&D: Functions Declaration: Declaration: int myFunction (int x, int y); Definition Definition int myFunction (int x, int y) { return x + y; return x + y;} The declaration provides only the header. the definition adds the body. The declaration provides only the header. the definition adds the body.

D&D: Data Types Data types in C++ are declared, but never defined. Data types in C++ are declared, but never defined. These are declarations: These are declarations: typedef float Weight; typedef string* StringPointer; enum Colors {red, blue, green}; Later we will look at these type declarations Later we will look at these type declarations struct S { … }; class C { … };

4. Organizing Decls & Defs into Files

5. Project Directories & Libraries