Separate Compilation.

Slides:



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

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.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 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++
chap13 Chapter 13 Programming in the Large.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
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.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
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.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
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.
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
C++ First Steps.
What Is? function predefined, programmer-defined
Topic Pre-processor cout To output a message.
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
Compilation and Debugging
Compilation and Debugging
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter Structured Types, Data Abstraction and Classes
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.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Learning Objectives What else in C++ Bitwise operator
Variables with Memory Diagram
Preprocessor C program → Modified C program → Object Code
Separate Compilation and Namespaces
Chapter 5 Function Basics
Announcements Midterm2 Grades to be announced NEXT Monday
6 Chapter Functions.
Functions How to reuse code.
Introduction to Classes and Objects
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
Announcements Final will be NEXT WEEK on August 17
Programming Introduction to C++.
C Preprocessor Seema Chandak.
Functions Lecture 5.
Separate Compilation.
What Is? function predefined, programmer-defined
CS1201: Programming Language 2
Class rational part2.
SPL – PS1 Introduction to C++.
Presentation transcript:

Separate Compilation

A key concept in programming Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython, Javascript…) It was a ‘milestone’ for procedural programming, some old ones like Pascal cannot, … It is in fact a kind of ‘abstract data type’, to separate the function “implementations” from its “definitions” It is also impossible for large projet without it

Translating a Library Program Source File Program Object File C++ Compiler .o g++ -c Library Header File Linker Program Executable File C++ Compiler .o e.g., g++ foo.cpp bar.o fb.o Library Implementation File Library Object File g++ -c 3

Motivation #include <iostream> using namespace std; bool even(int); bool odd(int x) { bool res; if(x == 0) res=false; else even(x-1); return res; } bool even(int x) { if (x == 0) res=true; else odd(x-1); int main() int x; cin >> x; cout >> odd(x) >> endl; cout >> even(x) >> endl; return 0;

It’s about ‘Organization’, organization of files! #include <iostream> using namespace std; bool even(int); bool odd(int); int main() { int x; cin >> x; cout >> odd(x) >> endl; cout >> even(x) >> endl; return 0; } bool odd(int x) bool res; if(x == 0) res=false; else even(x-1); return res; bool even(int x) { if (x == 0) res=true; else odd(x-1); It’s about ‘Organization’, organization of files!

One file  many files … 3 functions: odd() even() main() even() main() “main.cpp” even() “even.cpp” main() “main.cpp” odd() “odd.cpp” Reasons: 1. Reuse code for other programs 2. Divide work among programmers 3. Better maintenance, localize changes

odd() even() main() “odd.cpp” “even.cpp” “main.cpp” } #include <iostream> using namespace std; bool odd(int); bool even(int); int main() { int x; while (cin >> x) num_calls = 0; cout << boolalpha << odd(x) << endl; } return 0; #include <iostream> using namespace std; bool odd(int); bool even(int x) { … } #include <iostream> using namespace std; bool even(int); bool odd(int x) { … }

g++ file.cpp  a.out g++ -c main.cpp  main.o g++ -c even.cpp  even.o g++ -c odd.cpp  odd.o g++ -o odd-even main.o even.o odd.o  odd-even -c generates an object file -o generates an executable by linking all object files

Separate compilation by ‘makefile’ If only “main.cpp” is modified, just need to re-compile “main.cpp” and re-link by: g++ -c main.cpp g++ -o odd-even main.o even.o odd.o No need to re-compile the object files, i.e. even.o and odd.o ‘makefile’ will automate all these things!!! ‘makefile’ is also a program in ‘script’ language that organizes your ‘command files’!

Using header files Many declarations are repeated in “odd.cpp” and “even.cpp”, but We do not want to repeat writing the same declarations in multiple files. Should a declaration require updating, one has to go through all files that have the declaration and make the change. More importantly, maintaining duplicate information in multiple files is error-prone.

#include preprocessor directive include header files Instructs C++ preprocessor to replace directive with a copy of the contents of the specified file Quotes for user-defined header files Preprocessor first looks in current directory If the file is not found, looks in C++ Standard Library directory Angle brackets for C++ Standard Library Preprocessor looks only in C++ Standard Library directory #include <iostream> 11

Example of headfile myInclude.h main.cpp odd.cpp even.cpp #include <iostream> using namespace std; bool even(int); bool odd(int x) myInclude.h main.cpp odd.cpp even.cpp #include “myInclude.h” int main() { int x; cin >> x; cout >> odd(x) >> endl; cout >> even(x) >> endl; return 0; } #include “myInclude.h” bool even(int x) { bool res; if (x == 0) res=true; else odd(x-1); return res; } #include “myInclude.h” bool odd(int x) { bool res; if(x == 0) res=false; else even(x-1); return res; }

Avoiding recursive inclusions … It is ok to have multiple declarations of a function prototype, but not for its definition In the .h file, put the prototypes there .h files are likely to be multiply-included In creating the .o file, there may be nested #include statement The nested #include statement may be recursive In main.cpp, #include “foo.h” In foo.h, #include “bar.h” In bar.h, #include “foo.h” To break the infinite “recursive” inclusion, use #ifndefine #define to define a “variable” in the compilation process of .o file If a variable has been defined, the compiler will skip the code segment between #ifndefine and #endif. 13

#define preprocessor directive if XYZ is undefined, we process the file. Otherwise we do not process the file (jumping to the #endif). XYZ should not appear in any other files. #ifndef XYZ #define XYZ f1(); f2(); … #endif 14

File scope: external/internal Each file must know the existence of every variable, constant and function that it uses! Global constants: repeat their definitions (global constants are ‘local’ to the file, internal!) External (global) variables: add keyword “extern” External functions: add function prototypes (with or without “extern”) “extern”  it’s (just) a declaration, but not a definition! equivalently, it means the variable / function is global and is defined in another file.

library.h main.cpp source.cpp Output:Hello 199 extern int Int; int f(int); main.cpp source.cpp #include <iostream> #include "library.h" using namespace std; int Int=99; int main(){ cout << "Hello"<<endl; cout << f(100) << endl; return 0; } #include "library.h" int f(int i){ return (Int + i); } Output:Hello 199 16