David Notkin Autumn 2009 CSE303 Lecture 20 Multi-file (larger) programs Friday: social implications.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Week 5 - Wednesday.  What did we talk about last time?  Arrays.
 The position of a method in a program is not critical.  Why?
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Josh Goodwin
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Guide To UNIX Using Linux Third Edition
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
UNIT 13 Separate Compilation.
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.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Separate Compilation make and makefiles
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
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.
C++ Functions A bit of review (things we’ve covered so far)
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
C – Multi-file development and make
Executable program (p1)
Large Program Management: Make; Ant
Functions Separate Compilation
CS1010 Programming Methodology
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.
CS1010 Programming Methodology
Pre-processor Directives
Large Program Management: Make; Ant
Large Program Management: Make; Ant
CSE 390 Lecture 8 Large Program Management: Make; Ant
Code Organization CSCE 121 J. Michael Moore.
Writing Large Programs
Large Program Management: Make; Ant
Large Program Management: Make; Ant
What Is? function predefined, programmer-defined
Large Program Management: Make; Ant
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Executable program (p1)
CSE 390 Lecture 8 Large Program Management: Make; Ant
SPL – PS3 C++ Classes.
Large Program Management: Make; Ant
Presentation transcript:

David Notkin Autumn 2009 CSE303 Lecture 20 Multi-file (larger) programs Friday: social implications

Motivation Single-file programs do not work well when code gets large –compilation can be slow –hard to collaborate between multiple programmers –more cumbersome to edit So, larger programs are split into multiple files –each file represents a partial program or module –modules can be compiled separately or together –a module can be shared between multiple programs CSE303 Au092

Partial programs A.c file can contain a partial program: #include void f1(void) { // part1.c printf("this is f1\n"); } Such a file cannot be compiled into an executable by itself: $ gcc part1.c /usr/lib/gcc/crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status CSE303 Au093

But part2.c wants to use part1.c‘s code? #include void f2(void); // part2.c int main(void) { f1(); // not defined! f2(); } void f2(void) { printf("this is f2\n"); } The program will not compile –$ gcc -o combined part2.c –In function `main': –part2.c:6: undefined reference to `f1' CSE303 Au094

Including.c files (bad) One solution: #include part1.c in part2.c #include #include "part1.c" // note "" not <> void f2(void); int main(void) { f1(); // defined in part1.c f2(); } void f2(void) { printf("this is f2\n"); } The program will compile successfully: $ gcc -g -Wall -o combined part2.c CSE303 Au095

Multi-file compilation #include void f2(void); // part2.c int main(void) { f1(); // not defined? f2(); } void f2(void) { printf("this is f2\n"); } gcc accepts multiple source files to combine $ gcc -g -Wall -o combined part1.c part2.c $./combined this is f1 this is f2 CSE303 Au096

Object (.o) files A partial program can be compiled into an object (.o ) file with -c $ gcc -g -Wall -c part1.c $ ls part1.c part1.o part2.c A.o file is a binary blob of compiled C code that cannot be directly executed, but can be directly inserted into a larger executable later You can compile a mixture of.c and.o files $ gcc -g -Wall -o combined part1.o part2. Avoids recompilation of unchanged partial program files CSE303 Au097

The compilation process Each step's output can be dumped to a file, depending on arguments passed to gcc CSE303 Au098

Problem With the previous code, we can't safely create part2.o $ gcc -g -Wall -c part2.c part2.c: In function `main': part2.c:6: warning: implicit declaration of function `f1‘ The compiler is complaining because f1 does not exist. –But it will exist once part1.c/o is added in later We'd like a way to be able to declare to the compiler that certain things will be defined later in the compilation process... CSE303 Au099

Header files Header : A file whose only purpose is to be included –By convention a filename with the.h extension –Holds shared variables, types, and function declarations Key ideas: –every name.c intended to be a module has a name.h –name.h declares all global functions/data of the module –other.c files that want to use the module will #include name.h Some conventions: –.c files never contain global function prototypes –.h files never contain definitions (only declarations) –never #include a.c file (only.h files) –any file with a.h file should be able to be built into a. o file CSE303 Au0910

Multiple inclusion If multiple modules include the same header, the variables/functions in it will be declared twice Solution : use preprocessor to introduce conditional compilation –convention: #ifndef/#define with a variable named like the.h file –first time file is included, the variable won't be defined –on inclusions by other modules, will be defined and thus not included again #ifndef _FOO_H #define _FOO_H... // contents of foo.h #endif CSE303 Au0911

Global visibility // example.c int passcode = 12345; // example2.c int main(void) { printf("Password is %d\n", passcode); return 0; } By default, global variables and functions defined in one module can be seen and used by other modules it is compiled with –problem : gcc compiles each file individually before linking them –if example2.c is compiled separately into a.o file, its reference to passcode will fail as being undeclared CSE303 Au0912

extern // example2.c extern int passcode;... printf("Password is %d\n", passcode); extern used on variables and functions –does not actually define a variable/function or allocate space for it – but promises the compiler that some other module will define it –allows your module to compile even with an undeclared variable/function reference, so long as eventually its.o object is linked to some other module that declares that variable/function if example.c and example2.c are linked together, the above will work CSE303 Au0913

static // example.c int passcode = 12345; // public static int admin_passcode = 67890; // private static used on global variables and functions –visible only to the current file/module (sort of like Java's private ) –declare things static if you do not want them exposed –avoids potential conflicts with multiple modules that happen to declare global variables with the same names –passcode will be visible through the rest of example.c, but not to any other modules/files compiled with example.c CSE303 Au0914

Function static data When used inside a function static type name = value; … declares a static local variable that will be remembered across calls int nextSquare() { static int n = 0; static int increment = 1; n += increment; increment += 2; return n; } nextSquare() returns 1, then 4, then 9, then 16,... CSE303 Au0915

Risks File share leaks data on US Congress members under investigation Jeremy Epstein Fri, 30 Oct :54: The Washington Post's Oct 30 lead article notes that "more than 30 lawmakers and several aides" are under investigation for various possible misdeeds associated with "defense lobbying and corporate influence peddling". What's technology relevant is that the information leaked because a report was (presumably accidentally) placed on an unprotected computer (not clear whether it was a web site, a file share, or something else). No word on whether the problem was a misconfiguration (i.e., mis-set file permissions, whether accidentally or intentionally) or due to a bug in software that allowed bypassing protections. No indication that the data was encrypted... perhaps this is an opportunity for Congress to learn the need for more usable security systems, including encryption, to reduce the RISK of accidental sharing? CSE303 Au0916

Questions? CSE303 Au0917