SNU OOPSLA Lab. 9. Source Files and Programs © copyright 2001 SNU OOPSLA Lab.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
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.
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)
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
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.
Structure of a C program
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes 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.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Namespaces. Calculator Case-study Consider a simple desk calculator. It can be viewed as being composed of four parts: –The lexer, composing tokens out.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming in C++
C++ Code Analysis: an Open Architecture for the Verification of Coding Rules Paolo Tonella ITC-irst, Centro per la Ricerca Scientifica e Tecnologica
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Defining New Types Lecture 21 Hartmut Kaiser
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
Engineering Computing I Chapter 4 Functions and Program Structure.
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 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
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.
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.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
SNU OOPSLA Lab. Chap8. Namespaces and Exceptions © copyright 2001 SNU OOPSLA Lab.
Advanced BioPSE NCRR Programming Conventions and Standards J. Davison de St. Germain Chief Software Engineer SCI Institute December 2003 J.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
C++ Lesson 1.
What Is? function predefined, programmer-defined
Executable program (p1)
Chapter 6 CS 3370 – C++ Functions.
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
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.
Pre-processor Directives
Instructor: Ioannis A. Vetsikas
Separate Compilation and Namespaces
C Preprocessor(CPP).
C++ Compilation Model C++ is a compiled language
What Is? function predefined, programmer-defined
Executable program (p1)
SPL – PS1 Introduction to C++.
Presentation transcript:

SNU OOPSLA Lab. 9. Source Files and Programs © copyright 2001 SNU OOPSLA Lab.

SNU OOPSLA Lab. C++ Contents Separate Compilation Linkage Using Header Files Programs

SNU OOPSLA Lab. C++ 1. Separate Compilation(1) Having a complete program in one file is usually impossible The way a program is organized into files can help emphasize its logical structure, help a human reader understand the program, and help the compiler to enforce that logical structure The amount of time spent recompiling can be significantly reduced

SNU OOPSLA Lab. C++ To enable separate compilation, the programmer must supply declarations providing the type information needed to analyze a translation unit in isolation from the rest of the program 1. Separate Compilation(2) source file compiler separately compiled parts linker result of preprocessing

SNU OOPSLA Lab. C++ 2. Linkage(1) Names of functions, classes, templates, variables, namespaces, enumerations, and enumerators must be used consistently across all translation units unless they are explicitly specified to be local // file1.c: int x = 1; int f() { /* do something */ } // file2.c: extern int x; int f(); void g() { x = f(); } indicates that the declaration of x in file2.c is a declaration and not a definition

SNU OOPSLA Lab. C++ An object must be defined exactly once in a program it may be declared many times, but the types must agree exactly 2. Linkage(2) // file1.c: int x = 1; int b = 1; extern int c; // file2.c: int x;// meaning int x = 0; extern double b; extern int c; x is defined twice b is defined twice with different types c is declared twice but not defined Three errors

SNU OOPSLA Lab. C++ External linkage : a name that can be used in translation units different from the one in which it was defined Internal linkage : a name that can be referred to only in the translation unit in which it is defined an inline function must be defined in every translation unit in which it is used 2. Linkage(3)

SNU OOPSLA Lab. C++ 2. Linkage(4) consts and typedefs have internal linkage A const can be given external linkage by an explicit declaration // file1.c: extern const int a = 77; // file2.c: extern const int a; void g() { cout << a << ‘\n’; } 77

SNU OOPSLA Lab. C Header Files(1) One imperfect but simple method of achieving consistency for declarations in different translation units is to #include header files containing interface information in source files containing executable code and/or data definitions #include // from standard include directory #include “myheader.h”// from current directory

A header may contain 2.1 Header Files(2) Named namespacesnamespace N { /*... */ } Type definitionsstruct Point { int x, y; }; Template declarationstemplate class Z; Template definitionstemplate class V { /*... */ }; Function declarationextern int strlen(const char*); Inline function definitionsinline char get() { return *p++; } Data declarationextern int a; Constant definitionsconst float pi = ; Enumerationsenum Light { red, yellow, green }; Name declarationsclass Matrix; Include directives#include Macro definitions#define VERSION 12 Conditional compilation directives#ifdef_cplusplus Comments/* check for end of file */

SNU OOPSLA Lab. C++ A header should never contain 2.1 Header Files(3) Ordinary function definitionschar get() { return *p++; } Data definitionsint a; Aggregate definitionsshort tbl[] = { 1, 2, 3 }; Unnamed namespacesnamespace { /*... */ } Exported template definitionsexport template f(T t) { /*... */ }

SNU OOPSLA Lab. C Standard Library Headers The facilities of the standard library are presented through a set of standard headers #ifdef _cplusplus// include guard namespace std {// the standard library is defined in namespace std extern “C” {// stdio functions have C linkage #endif //... int printf(const char*...); //... #ifdef _cplusplus } using namespace std;// make stdio available in global namespace #endif

SNU OOPSLA Lab. C The One-Definition Rule(1) A given class, enumeration, and template, etc., must be defined exactly once in a program ODR - “the one-definition rule” Two definitions of a class, template, or inline function are accepted as examples of the same unique definition if and only if they appear in different translation units they appear token-for-token identical the meaning of those tokens are the same in both translation units

SNU OOPSLA Lab. C++ The intent of the ODR is to allow inclusion of a class definition in different in different translation units from a common source file 2.3 The One-Definition Rule(2) // file s.h: struct S { int a; char b; }; void f(S*); // file1.c: #include “s.h” // use f() here // file2.c: #include “s.h” void f(S* p) { /*... */ } struct S { int a; char b; }; void f(S*); #include “s.h” // use f() here #include “s.h” void f(S* p) { /*... */ } file1.c:file2.c: s.h:

Three ways of violating the ODR 2.3 The One-Definition Rule(3) // file1.c: struct S1 { int a; char b; }; // file1.c: typedef int X; struct S3 { X a; char b; }; // file2.c: typedef char X; struct S3 { X a; char b; }; struct may not be defined twice in a single file // file1.c: struct S2 { int a; char b; }; // file2.c: struct S2 { int a; char bb; }; S2 is used to name classes that differ in a member name two definitions of S3 are token-for-token identical meaning of the name X has sneakily been made to differ in two files

SNU OOPSLA Lab. C Linkage to Non-C++ Code(1) A C++ program contains parts written in other languages Cooperation can be difficult between program fragments written in different languages and even between fragments written in the same language but compiled with different compilers To help, one can specify a linkage convention to be used in an extern declaration

SNU OOPSLA Lab. C Linkage to Non-C++ Code(2) extern “C” char* strcpy(char*, const char*); C and C++ standard library function strcpy() and specifies that it should be linked according to the C linkage conventions The extern “C” directive is particularly useful because of the close relationship between C and C++ The C in extern “C” names a linkage convention and not a language

SNU OOPSLA Lab. C++ An extern “C” directive specifies the linkage convention (only) and does not affect the semantics of calls to the function A function declared extern “C” still obeys the c++ type checking and argument conversion rules and not the weaker C rules 2.4 Linkage to Non-C++ Code(3) extern “C” int f(); int g() { return f(1);// error: no argument expected }

SNU OOPSLA Lab. C++ Specify linkage to a group of declarations : linkage block 2.4 Linkage to Non-C++ Code(4) extern “C” { char* strcpy(char*, const char*); int strcmp(const char*, const char*); //... } extern “C” { #include } enclose a complete C header to make a header suitable for C++ use #ifdef _cplusplus extern “C” { #endif char* strcpy(char*, const char*); int strcmp(const char*, const char*); //... #ifdef _cpluscplus } #endif conditional compilation can be used to create a common C and C++ header

SNU OOPSLA Lab. C++ Any declaration can appear within a linkage block A name with C linkage can be declared in a namespace 2.4 Linkage to Non-C++ Code(5) extern “C” {// any declaration here, for example: int g1;// definition extern int g2;// declaration, not definition } #include void f() { std::printf(“Hello, “);// ok printf(“world!\n”);// error: no global printf() }

SNU OOPSLA Lab. C++ 3. Using Header Files A few alternative ways of expressing the physical structure of the calculator program

SNU OOPSLA Lab. C Single Header File(1) Simplest solution : put the definition in a suitable number of.c files and to declare the types needed for them to communicate in a single.h file that each.c file #includes five.c files(lexer.c, parser.c, table.c error.c, main.c) + header dc.h It is most useful when the program is small and its parts are not intended to be used separately

SNU OOPSLA Lab. C++ // dc.h: namespace Error { struct Zero_divide { }; struct Syntax_error { const char* p; Syntax_error(const char* q) { p = q; } }; } #include namespace Lexer { enum Token_value { NAME,NUMBER,END, PLUS=‘+’,MINUS=‘-’,MUL=‘*’,DIV=‘/’, PRINT=‘;’,ASSIGN=‘=‘,LP=‘(‘,RP=‘)’ }; extern Token_value curr_tok; extern double number_value; extern std::string string_value; Token_value get_token(); }// continued...

SNU OOPSLA Lab. C++ namespace Parser { double prim(bool get);// handle primaries double term(bool get);// multiply and divide double expr(bool get);// add and subtract using Lexer::get_token; using Lexer::curr_tok; } #include extern std::map table; namespace Driver { extern int no_of _errors; extern std::istream* input; void skip(); }

SNU OOPSLA Lab. C++ // lexer.c: #include “dc.h” #include Token_value Lexer::curr_tok; double Lexer::number_value; std::string Lexer::string_value; Lexer::Token_value Lexer::get_token() { /*... */ } // parser.c: #include “dc.h” double Parser::prim(bool get) { /*... */ } double Parser::term(bool get) { /*... */ } double Parser::expr(bool get) { /*... */ } // table.c: #include “dc.h” std::map table; // main.c: #include “dc.h” #include int Driver::no_of_errors = 0; std::istream* Driver::input = 0; void Driver::skip() { /*... */ } int main(int argc, char* argv[]) { /*... */ }

SNU OOPSLA Lab. C Single Header File(2) dc.h driver.cparser.ctable.clexer.c

SNU OOPSLA Lab. C Multiple Header Files(1) Each logical module have its own header defining the facilities it provides Each.c file then has a corresponding.h file specifying what it provides Each.c file includes it own.h file and usually also other.h files that specify what it needs from other modules in order to implement the services advertised in the interface Makes it easy to determine exactly what the parser code depends on and to ignore the rest of the program

SNU OOPSLA Lab. C++ // parser.h: namespace Parser {// interface for users double expr(bool get); } // parser_impl.h: #include “parser.h” #include “error.h” #include “lexer.h” namespace Parser {// interface for implementers double prim(bool get); double term(bool get); double expr(bool get); using Lexer::get_token; using Lexer::curr_tok; } // parser.c: #include “parser_impl.h” #include “table.h” double Parser::prim(bool get) {/*...*/ } double Parser::term(bool get) {/*...*/ } double Parser::expr(bool get) {/*...*/}

SNU OOPSLA Lab. C++ parser_impl.h parser.hlexer.herror.htable.h parser.c driver.c the parser and the driver 3.2 Multiple Header Files(2)

SNU OOPSLA Lab. C Multiple Header Files(3) parser_impl.h parser.hlexer.herror.htable.h parser.c the parser expr.c keen on minimizing dependencies

SNU OOPSLA Lab. C Include Guards(1) The idea of the multiple-header approach is to represent each logical module as a consistent, self- contained unit Viewed from the program as a whole, many of the declaration needed to make each logical module complete are redundant reorganize our program to remove the redundancy find a way to allow repeated inclusion of headers

SNU OOPSLA Lab. C++ Insert include guards in headers 3.3 Include Guards(2) // error.h: #ifndef CALC_ERROR_H #define CALC_ERROR_H namespace Error { //... } #endif// CALC_ERROR_H

SNU OOPSLA Lab. C++ 4. Programs A collection of separately compiled units by a linker Every function, object, type, etc., used in this collection must have a unique definition The program must contain exactly one function called main()

SNU OOPSLA Lab. C Initialization of Nonlocal Variables A variable defined outside any function (that is, global, namespace, and class static variables) is initialized before main() is invoked Such nonlocal variables in a translation unit are initialized in their declaration order There is no guaranteed order of initialization of global variables in different translation units

SNU OOPSLA Lab. C Program Termination A program can terminate in several ways by returning from main() by calling exit() by calling abort() by throwing an uncaught exception If a program is terminated using the standard library function exit(), the destructors for constructed static objects are called (not abort()) Calling exit() means that the local variables of the calling function and its callers will not have their destructors invoked