+ All Categories
Home > Documents > The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3....

The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3....

Date post: 31-Dec-2015
Category:
Upload: jessie-lang
View: 215 times
Download: 1 times
Share this document with a friend
25
The Structure of a The Structure of a C++ Program C++ Program
Transcript
Page 1: The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.

The Structure of a C+The Structure of a C++ Program+ Program

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

OutlineOutline

1.1. Separate CompilationSeparate Compilation

2.2. The # PreprocessorThe # Preprocessor

3.3. Declarations and DefinitionsDeclarations and Definitions

4.4. Organizing Decls & Defs into FilesOrganizing Decls & Defs into Files

5.5. Project Directories & LibrariesProject Directories & Libraries

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

1. Separate Compilation1. Separate Compilation

C++ programs can range from a C++ programs can range from a handful of statements to hundreds of handful of statements to hundreds of thousandsthousands

May be written by one person or by a May be written by one person or by a teamteam

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

Single File ProgramsSingle File Programs OK for small programs OK for small programs

(CS150)(CS150) But with large programs:But with large programs:

– compilation would take compilation would take minutes, hours, maybe minutes, hours, maybe daysdays

might break compilermight break compiler– Team members would Team members would

interfere with one interfere with one another’s work.another’s work.

““Are you still editing that Are you still editing that file? You’ve had it all file? You’ve had it all afternoon.”afternoon.”

““What do you mean What do you mean you’reyou’re saving changes to saving changes to the file? the file? I’veI’ve been editing been editing it for the last 45 minutes!”it for the last 45 minutes!”

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

Multiple File C++ ProgramsMultiple File C++ Programs

Team members can work in parallel Team members can work in parallel on separate fileson separate files

Files are compiled separatelyFiles are compiled separately– each individual compilation is fasteach individual compilation is fast

Separately compiled code is Separately compiled code is linkedlinked to to produce the executableproduce the executable– linking is much faster than compilationlinking is much faster than compilation

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

Separate CompilationSeparate Compilation Each file of Each file of source codesource code

– programming language programming language texttext

is compiled to produce a is compiled to produce a file of file of object codeobject code– binary code, almost binary code, almost

executableexecutable– exact addresses of exact addresses of

variables and functions variables and functions not known, represented not known, represented by symbolsby symbols

All object code files All object code files linkedlinked to produce the to produce the executableexecutable– Linking mainly consists of Linking mainly consists of

replacing symbols by real replacing symbols by real addresses.addresses.

A .cpp

var iable s : afunc tion s : fa

c al l s : fb (a )

B .cpp

var iable s :func tion s : fb

c al l s : fb (1)

C .cpp

var iable s : bfunc tion s : ma in

c al l s : fa (a ,b ), fb (a ), fb (b )

A .o

a : x1000fa : x12A 0

c al l s : fb (x1000)

B .o

fb : x2000

us e s : x2000(1)

C .o

b : x3000ma in : x3400

c al l s : fa (a ,x3000), fb (a ),fb (x3000)

foo .e x ex 2 0 0 0 ( x 1 0 0 0 )

x 2 0 0 0 ( 1 )x 1 2 a0 ( X 1 0 0 0 ,X 3 0 0 0 ) ,

X 2 0 0 0 ( X 1 0 0 0 ) , X 2 0 0 0 ( X 3 0 0 0 )

C O M P ILIN G

LIN K IN G

S o u rc eC o d e

O b je c tC o d e

E xe c u ta b le

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

A .cpp

var iable s : afunc tion s : fa

c al l s : fb (a )

B .cpp

var iable s :func tion s : fb

c al l s : fb (1)

C .cpp

var iable s : bfunc tion s : ma in

c al l s : fa (a ,b ), fb (a ), fb (b )

A .o

a : x1000fa : x12A 0

c al l s : fb (x1000)

B .o

fb : x2000

us e s : x2000(1)

C .o

b : x3000ma in : x3400

c al l s : fa (a ,x3000), fb (a ),fb (x3000)

foo .e x ex 2 0 0 0 ( x 1 0 0 0 )

x 2 0 0 0 ( 1 )x 1 2 a0 ( X 1 0 0 0 ,X 3 0 0 0 ) ,

X 2 0 0 0 ( X 1 0 0 0 ) , X 2 0 0 0 ( X 3 0 0 0 )

C O M P ILIN G

LIN K IN G

S o u rc eC o d e

O b je c tC o d e

E xe c u ta b le

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

Large ProjectsLarge Projects

On large projects with hundreds to On large projects with hundreds to thousands of filesthousands of files

Typically only a few files are changed Typically only a few files are changed on any one dayon any one day

Often only the changed files need to Often only the changed files need to be recompiledbe recompiled

Then link the changed and Then link the changed and unchanged object codeunchanged object code

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

2. The # Preprocessor2. The # Preprocessor

The The preprocessorpreprocessor runs before the compiler runs before the compiler properproper– modifies the source codemodifies the source code– processes preprocessor instructionsprocesses preprocessor instructions

lines beginning with #lines beginning with #

– strips out commentsstrips out comments

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

Processor InstructionsProcessor Instructions

#include#include– insert a fileinsert a file

#define#define– define a macrodefine a macro

#ifdef, #ifndef, #endif#ifdef, #ifndef, #endif– check to see if a macro has been definedcheck to see if a macro has been defined

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

#include#include

Inserts a file or header into the Inserts a file or header into the current source codecurrent source code

Two versionsTwo versions– #insert <headerName>#insert <headerName>

inserts a system header file from a location inserts a system header file from a location defined when the compiler was installeddefined when the compiler was installed

– #insert “fileName”#insert “fileName” inserts a file from the current directoryinserts a file from the current directory

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

#include Example#include Example

Suppose we have three files: Suppose we have three files: A.hA.h, , B.hB.h, , C.cppC.cpp

We ask the compiler to only run the We ask the compiler to only run the preprocessor and save the result:preprocessor and save the result:g++ -E C.cpp > C.ig++ -E C.cpp > C.i

The result is file The result is file C.iC.i– Note the presence of content from all three Note the presence of content from all three

filesfiles includes markers telling where the content came includes markers telling where the content came

fromfrom

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

#include Example 2#include Example 2

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

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

#define#define Used to define macros (symbols that the Used to define macros (symbols that the

preprocessor will later substitute for)preprocessor will later substitute for)– Sometimes used to supply constantsSometimes used to supply constants#define VersionNumber "1.0Beta1"#define VersionNumber "1.0Beta1"

int main() {int main() { cout << "Running version “cout << "Running version “ << VersionNumber<< VersionNumber << endl;<< endl;

Much more elaborate macros are possible, Much more elaborate macros are possible, including ones with parametersincluding ones with parameters

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

#ifdef, #ifndef, #endif#ifdef, #ifndef, #endif

Used to select code based upon whether a Used to select code based upon whether a macro has been defined:macro has been defined:

#ifdef __GNUG__#ifdef __GNUG__

/* Compiler is gcc/g++ *//* Compiler is gcc/g++ */

#endif#endif

#ifdef _MSC_VER#ifdef _MSC_VER

/* Compiler is Microsoft Visual C++ /* Compiler is Microsoft Visual C++ */*/

#endif#endif

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

#if, #define, and #include#if, #define, and #include

All of these macros are used to reduce the All of these macros are used to reduce the amount of code seen by the actual compileramount of code seen by the actual compiler

Suppose we have three files: Suppose we have three files: A2.hA2.h, , B2.hB2.h, , C2.cppC2.cpp We ask the compiler to only run the We ask the compiler to only run the

preprocessor and save the result:preprocessor and save the result:g++ -E C2.cpp > C2.ig++ -E C2.cpp > C2.i

The result is file The result is file C2.iC2.i– Note that the code from A2.h is included only onceNote that the code from A2.h is included only once– Imagine now, if that were Imagine now, if that were iostreamiostream instead of instead of A2.hA2.h

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

3. Declarations and 3. Declarations and DefinitionsDefinitions

Some of the most common error Some of the most common error messages aremessages are– … … is undeclaredis undeclared– … … is undefinedis undefined– … … is defined multiple timesis defined multiple times

Fixing these requires that you Fixing these requires that you understand the difference between understand the difference between declarations and definitionsdeclarations and definitions– and how they relate to the program and how they relate to the program

structurestructure

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

DeclarationsDeclarations

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

to use itto use it

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

DefinitionsDefinitions

A definition in C++ A definition in C++ – introduces or repeats a name for introduces or repeats a name for

somethingsomething– tells what “kind” of thing it istells what “kind” of thing it is– tells what value it has and/or how it tells what value it has and/or how it

worksworks– gives the compiler enough information gives the compiler enough information

to generate this and assign it an addressto generate this and assign it an address

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

General Rules for Decls & General Rules for Decls & DefsDefs

All definitions are also declarations.All definitions are also declarations.– But not vice-versaBut not vice-versa

A name must be declared before you can A name must be declared before you can write any code that uses it.write any code that uses it.

A name can be declared any number of A name can be declared any number of times, as long as the declarations are times, as long as the declarations are identical.identical.

A name must be defined exactly once, A name must be defined exactly once, somewhere within all the separately somewhere within all the separately compiled files making up a program.compiled files making up a program.

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

D&D: VariablesD&D: Variables

These are definitions of variables:These are definitions of variables:int x;int x;

string s = “abc”;string s = “abc”;

MyFavoriteDataType mfdt (0);MyFavoriteDataType mfdt (0); These are declarations:These are declarations:

extern int x;extern int x;

extern string s;extern string s;

extern MyFavoriteDataType mfdt;extern MyFavoriteDataType mfdt;

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

D&D: FunctionsD&D: Functions

Declaration:Declaration:int myFunction (int x, int y);int myFunction (int x, int y);

DefinitionDefinitionint myFunction (int x, int y)int myFunction (int x, int y){{ return x + y;return x + y;}}

The declaration provides only the The declaration provides only the header. the definition adds the body.header. the definition adds the body.

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

D&D: Data TypesD&D: Data Types

Data types in C++ are declared, but never Data types in C++ are declared, but never defined.defined.

These are declarations:These are declarations:typedef float Weight;typedef float Weight;

typedef string* StringPointer;typedef string* StringPointer;

enum Colors {red, blue, green};enum Colors {red, blue, green}; Later we will look at these type Later we will look at these type

declarationsdeclarationsstruct S { … };struct S { … };

class C { … };class C { … };

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

4. Organizing Decls & Defs into 4. Organizing Decls & Defs into FilesFiles

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

5. Project Directories & 5. Project Directories & LibrariesLibraries


Recommended