M The University Of Michigan Andrew M. Morgan EECS498-006 Lecture 09 Savitch Ch. 11.1 Compiling With Multiple Files Make Utility.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
M The University Of Michigan Andrew M. Morgan EECS Lecture 14 Savitch Ch. 9.0 C++ String Data Type C-Strings.
M The University Of Michigan Andrew M. Morgan EECS Lecture 25 Savitch Ch. 15 Polymorphism.
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,
M The University Of Michigan Andrew M. Morgan EECS Lecture 06 Savitch Ch. 5 Arrays Multi-Dimensional Arrays.
M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
Savitch N/A Randomness In Programming Simulations
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 05 Savitch Ch Streams Stream States Input/Output.
M The University Of Michigan Andrew M. Morgan EECS Lecture 18 Savitch Ch. 17 Linked Data Structures.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
CSE Lecture 10 – Functions
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C++ Classes & Data Abstraction
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Separate Compilation and Namespaces
Lecture 4-7 Classes and Objects
Separate Compilation and Namespaces
Classes and Data Abstraction
Name: Rubaisha Rajpoot
Screen output // Definition and use of variables
Code Organization Classes
Classes.
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
CS 144 Advanced C++ Programming February 21 Class Meeting
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Code Organization Classes
Presentation transcript:

M The University Of Michigan Andrew M. Morgan EECS Lecture 09 Savitch Ch Compiling With Multiple Files Make Utility

M M EECS498 EECS498 Andrew M Morgan2 Recall ADT Abstract Data Type (ADT): A data type, which has its implementation details hidden from the programmer using it –Programmer using ADT may not know what algorithms were used to implement the functions making up the interface of the ADT In C++, developing classes that have their member function implementations hidden outside the class definition results in an ADT How to actually separate the implementation details from the class interface?

M M EECS498 EECS498 Andrew M Morgan3 Separating Implementation From Interface Put implementation in a different file from interface –Interface is put into a header file, extension ".h" –Implementation is put into a source file, extension ".cpp" Provide to users (programmers using your class): –The header file containing the class interface Allows the user to know the functionality available, and how to use it –A compiled implementation, in the form of an object (.o) file Allows the user to link in object code into their program Allows users to make use of your class and all of its functionality User never sees implementation, since only object code is provided to them

M M EECS498 EECS498 Andrew M Morgan4 Example ADT Interface Consider the following interface for a CircleClass ADT –Written to a file called "CircleClass.h" class CircleClass { public: //Sets a circle's location and radius… void setAttributes(float inX, float inY, float inRad); float computeArea(); //Returns area of this circle object float computeCircumference(); //Returns circumference of circle void print(); //Prints out info about circle void translateX(int amt); //Change X position of the circle void translateY(int amt); //Change Y position of the circle private: float xLoc; float yLoc; float radius; };

M M EECS498 EECS498 Andrew M Morgan5 Using The CircleClass Interface Given CircleClass interface, this main() could be written: #include using namespace std; #include "CircleClass.h" int main(void) { CircleClass circ1; CircleClass circ2; circ1.setAttributes(0, 0, 1); circ2.setAttributes(0, 0, 1); circ1.translateX(50); circ1.translateY(50); cout << "circ1: " << endl; circ1.print(); cout << endl; cout << "circ2: " << endl; circ2.print(); return(0); } circ1: Circle: (50, 50) Radius: 1 circ2: Circle: (0, 0) Radius: 1 Implementation details were not needed to be able to write this function, or to determine what the results would be.

M M EECS498 EECS498 Andrew M Morgan6 The CircleClass Implementation #include using namespace std; #include "CircleClass.h" const float PI = ; //Sets a circle's location and radius void CircleClass::setAttributes( float inX, float inY, float inRad ) { xLoc = inX; yLoc = inY; radius = inRad; } //Returns area of this circle object float CircleClass::computeArea( ) { float area; area = PI * radius * radius; return (area); } //Continued, next column //Returns circumference of circle float CircleClass::computeCircumference( ) { float circumf; circumf = PI * (radius * 2); return (circumf); } //Prints out info about circle void CircleClass::print( ) { cout << "Circle: (" << xLoc << ", " << yLoc << ") Radius: " << radius << endl; } //Change X position of the circle void CircleClass::translateX( int amt ) { xLoc += amt; } //Change Y position of the circle void CircleClass::translateY( int amt ) { yLoc += amt; }

M M EECS498 EECS498 Andrew M Morgan7 Review: Creating An Executable With C++ Create C++ program with extension.cpp Pre-processor –"pastes" prototypes and definitions from include files –Controlled via pre-processor directives that begin with "#" –Results in C++ code that has been modified based on directives Compiler –Converts C++ code into assembly and/or machine language –Usually called "object code" with extension.o –Leaves "holes" in place of function calls from other libraries Linker –Fills holes from compiler with locations (addresses) of library functions –Results in complete sequence of machine language Result: Executable program –Can be executed on the platform in which it was compiled and linked –Sometimes, all steps are combined, so individual steps are transparent to user

M M EECS498 EECS498 Andrew M Morgan8 How To Build A Program Using An ADT For the CircleClass ADT example, there are 3 files –CircleClass.h: Contains interface for CircleClass –CircleClass.cpp: Contains implementation of CircleClass members –circleMain.cpp: Contains the main() function using CircleClass objects Building an executable program: –Compile CircleClass.cpp to create CircleClass.o –Compile circleMain.cpp to create circleMain.o –Link CircleClass.o and circleMain.o together to create circleMain.exe Stop g++ after compiling using the "-c" command-line parameter –g++ -c CircleClass.cpp –o CircleClass.o –g++ -c circleMain.cpp –o circleMain.o Link object files together by providing all.o files to g++ –g++ CircleClass.o circleMain.o –o circleMain.exe

M M EECS498 EECS498 Andrew M Morgan9 Use Of #include #include is a preprocessor directive Operation is performed prior to compilation –Essentially "pastes" the contents of the file being #included into the file containing the #include directive class IntClass { public: int val; }; #include "IntClass.h" int main() { IntClass iObj; iObj.val = 14; return (0); } IntClass.h example.cpp class IntClass { public: int val; }; int main() { IntClass iObj; iObj.val = 14; return (0); } Results of preprocessor:

M M EECS498 EECS498 Andrew M Morgan10 Potential #include Problem class IntClass { public: int val; }; #include "IntClass.h" #include "SecondClass.h" int main() { IntClass iObj; SecondClass scObj; iObj.val = 14; scObj.icObj.val = 6; scObj.fVar = 3.14; return (0); } IntClass.hexample.cpp #include "IntClass.h" class SecondClass { public: IntClass icObj; float fVar; }; SecondClass.h class IntClass { public: int val; }; class IntClass { public: int val; }; class SecondClass { public: IntClass icObj; float fVar; }; int main() { IntClass iObj; SecondClass scObj; iObj.val = 14; scObj.icObj.val = 6; scObj.fVar = 3.14; return (0); } Result of preprocessor General Rule: #include a header file in every file that uses something from it SecondClass.h uses the IntClass type, and therefore, should #include "IntClass.h" example.cpp uses both the IntClass type and the SecondClass type, and therefore should #include both "IntClass.h" and "SecondClass.h" Problem: Preprocessor results in IntClass being defined twice This will cause a compile-time error!!!

M M EECS498 EECS498 Andrew M Morgan11 Using #include Guards, Motivation Poor solution: Don't #include "IntClass.h" inside "SecondClass.h" –Since it will have already been #included in example.cpp by the time it is needed in SecondClass.h, this would actually work –However, it violates the general rule "#include a header file in every file that uses something from it" –What if I needed to use SecondClass in a later program that didn't #include "IntClass.h" first? Would not be able to use SecondClass.h since it would cause an error since IntClass is undefined If I modify SecondClass.h for this new program, then the original program in example.cpp won't compile because IntClass is multiply defined Other preprocessor directives can be used to solve the problem of #including a file multiple times

M M EECS498 EECS498 Andrew M Morgan12 New Preprocessor Directives #ifndef SYMBOL … #endif directives: –Read "if not defined" –Statements between these directives are considered if "SYMBOL" has not been previously defined –If "SYMBOL" has been previously defined, the preprocessor literally ignores the statements between these directives #define SYMBOL directive: –Used to define a symbol #ifndef _EXAMPLESYMBOL_ x = 18; #endif #define _EXAMP2_ #ifndef _EXAMP2_ y = 38; #endif x = 18; Result of preprocessorExample Code Since the "_EXAMPLESYMBOL_" had not been defined, the preprocessor does not skip the "x = 18;" line Since the "_EXAMP2_" had been defined, the "y=38;" statement is skipped in the preprocessor output

M M EECS498 EECS498 Andrew M Morgan13 Using #include Guards "#include Guards" can be used to ensure that the contents of a header file is not included in preprocessor output more than once –At the beginning of every header file, check if a symbol has been defined –If not, define it immediately and provide contents of header file –Next time header file is #included, the symbol will have been defined, and the contents of the header file can be ignored In this way, the header file contents are included the very first time, but not multiple times Allows the rule of #including a file every time it is needed to be followed

M M EECS498 EECS498 Andrew M Morgan14 Use Of #include Guards, Example #ifndef _INTCLASS_H_ #define _INTCLASS_H_ class IntClass { public: int val; }; #endif #include "IntClass.h" #include "SecondClass.h" int main() { IntClass iObj; SecondClass scObj; iObj.val = 14; scObj.icObj.val = 6; scObj.fVar = 3.14; return (0); } IntClass.h example.cpp #ifndef _SECONDCLASS_H_ #define _SECONDCLASS_H_ #include "IntClass.h" class SecondClass { public: IntClass icObj; float fVar; }; #endif SecondClass.h class IntClass { public: int val; }; class SecondClass { public: IntClass icObj; float fVar; }; int main() { IntClass iObj; SecondClass scObj; iObj.val = 14; scObj.icObj.val = 6; scObj.fVar = 3.14; return (0); } Result of preprocessor While the symbol for the #include guards can be any valid identifier, a common practice is to name them according the name of the header file, as shown, to prevent a symbol from getting used multiple times and incorrectly causing code not to be included.

M M EECS498 EECS498 Andrew M Morgan15 Intro To "make" Consider a program with 100 source files Each file needs to be compiled individually, and then linked together to form an executable –Requires 100 "g++ -c …" commands to be entered –During debugging, you may need to update and re-compile many times –You don't want to have to type in 100 commands every time! There is a utility in UNIX called "make" which helps to automate this process Developer must still develop a "Makefile" to tell the utility what needs to be done

M M EECS498 EECS498 Andrew M Morgan16 Dependencies and Makefiles If you only changed one source file, you may not need to re- compile all 100 files to build an executable –If the only file changed was the file containing main(), for example, only that file needs to be re-compiled –No other source files "depend on" the main() function –If a.cpp file uses an interface that has changed, the.cpp file must be re- compiled to be sure the changes in the interface don't cause errors in the way it is used The.cpp file is said to "depend on" the file containing the interface The make utility uses these dependencies to determine which files need to be re-compiled, and which don't

M M EECS498 EECS498 Andrew M Morgan17 Dependencies, Example #ifndef _INTCLASS_H_ #define _INTCLASS_H_ class IntClass { public: int val; }; #endif #include "IntClass.h" #include "SecondClass.h" int main() { IntClass iObj; SecondClass scObj; iObj.val = 14; scObj.icObj.val = 6; scObj.fVar = 3.14; return (0); } IntClass.h example.cpp #ifndef _SECONDCLASS_H_ #define _SECONDCLASS_H_ #include "IntClass.h" class SecondClass { public: IntClass icObj; float fVar; }; #endif SecondClass.h In this case, example.cpp "depends on" both IntClass.h and SecondClass.h If either of the.h files is changed, example.cpp must be re-compiled Consider if IntClass.h changed such that the data member "val" was renamed "theInt" Statements in main such as "iObj.val = 14;" are no longer valid, and re- compiling example.cpp will show these errors

M M EECS498 EECS498 Andrew M Morgan18 Makefile Syntax Makefiles are not written in C++! The basic syntax for a makefile is as follows: A "target" is just a label make uses to indicate what is to be built The dependency list continues on the same line as the target The command line must start with a TAB character –Can not be some number of spaces! –The command is a UNIX command that, when executed, brings the target "up-to-date" (i.e. compiled, linked, etc) target1: dependency1 dependency2... command to bring up to date target2:

M M EECS498 EECS498 Andrew M Morgan19 Example Makefile For Circle Project circleMain.exe: CircleClass.o circleMain.o g++ CircleClass.o circleMain.o -o circleMain.exe circleMain.o: circleMain.cpp g++ -c circleMain.cpp -o circleMain.o CircleClass.o: CircleClass.cpp CircleClass.h g++ -c CircleClass.cpp -o CircleClass.o clean: rm -rf circleMain.o CircleClass.o circleMain.exe In order for "circleMain.exe" to be brought up-to-date, both CircleClass.o and circleMain.o must be up-to-date. If they are, the way to bring circleMain.exe up-to-date is to link the.o files with the UNIX command "g++ CircleClass.o circleMain.o -o circleMain.exe". If they are not up-to-date, they will be brought up-to-date prior to issuing the command for the circleMain.exe target

M M EECS498 EECS498 Andrew M Morgan20 "Up-To-Date" In Makefiles The make utility can determine if something is "up-to-date" by comparing its timestamp –CircleClass.o depends on CircleClass.cpp and CircleClass.h –CircleClass.o is considered up-to-date if the CircleClass.o file is newer than both the CircleClass.h and CircleClass.cpp files This means that the.h and.cpp files have not changed since the.o file was created, and therefore, re-creating the.o file can not result in any difference Using the dependencies, the make utility will only spend time compiling files that need to be updated Time will not be spent compiling files that dont need to be compiled

M M EECS498 EECS498 Andrew M Morgan21 Using The make Utility Following are examples of using make –Red text indicates user issued commands UNIXprompt% make g++ -c CircleClass.cpp -o CircleClass.o g++ -c circleMain.cpp -o circleMain.o g++ CircleClass.o circleMain.o -o circleMain.exe UNIXprompt% UNIXprompt% make g++ -c circleMain.cpp -o circleMain.o g++ CircleClass.o circleMain.o -o circleMain.exe UNIXprompt% make clean rm -rf circleMain.o CircleClass.o circleMain.exe UNIXprompt% make g++ -c CircleClass.cpp -o CircleClass.o g++ -c circleMain.cpp -o circleMain.o g++ CircleClass.o circleMain.o -o circleMain.exe UNIXprompt% Note: CircleClass.o need not be re-compiled, since it does not depend on circleMain.cpp, which is all that has changed!