CSE 303 Lecture 16 Multi-file (larger) programs

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Introduction to C Programming
David Notkin Autumn 2009 CSE303 Lecture 20 Multi-file (larger) programs Friday: social implications.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
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.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
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
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
CS 261 – Data Structures Introduction to C Programming.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
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.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
What Is? function predefined, programmer-defined
C – Multi-file development and make
Executable program (p1)
Large Program Management: Make; Ant
Dynamic Memory Allocation
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
Large Program Management: Make; Ant
Large Program Management: Make; Ant
6 Chapter Functions.
CSE 390 Lecture 8 Large Program Management: Make; Ant
Code Organization CSCE 121 J. Michael Moore.
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Based on slides created by Bjarne Stroustrup & Tony Gaddis
What Is? function predefined, programmer-defined
Large Program Management: Make; Ant
Executable program (p1)
CSE 390 Lecture 8 Large Program Management: Make; Ant
SPL – PS3 C++ Classes.
Large Program Management: Make; Ant
Presentation transcript:

CSE 303 Lecture 16 Multi-file (larger) programs reading: Programming in C Ch. 15 slides created by Marty Stepp http://www.cs.washington.edu/303/

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 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

Partial programs A .c file can contain a partial program: #include <stdio.h> 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

Using a partial program We have part2.c that wants to use the code from part1.c: #include <stdio.h> 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 by itself: $ gcc -o combined part2.c In function `main': part2.c:6: undefined reference to `f1'

Including .c files (bad) One solution (bad style): include part1.c in part2.c #include <stdio.h> #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

Multi-file compilation #include <stdio.h> void f2(void); // part2.c int main(void) { f1(); // not defined? f2(); } void f2(void) { printf("this is f2\n"); The gcc compiler can accept multiple source files to combine: $ gcc -g -Wall -o combined part1.c part2.c $ ./combined this is f1 this is f2

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.c avoids recompilation of unchanged partial program files

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

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...

Header files header : A C file whose only purpose is to be included. generally 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

Exercise Write a program that can maintain a linked list of integers. You should have functions for printing a linked list and summing it. The main function should create a list, put some elements in it, and print/sum them. Appropriately divide your program into multiple .c and .h files. data next 10 data next 20 data next 30 NULL front

Multiple inclusion problem : 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 preprocessor won't be defined on inclusions by other modules, will be defined  not included again #ifndef _FOO_H #define _FOO_H ... // contents of foo.h #endif

Global visibility // example.c int passcode = 12345; // example2.c int main(void) { printf("Password is %d\n", passcode); return 0; } by default, global variables/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

extern extern (when used on variables/functions) : // example2.c extern int passcode; ... printf("Password is %d\n", passcode); extern (when used on variables/functions) : does not actually define a variable/function or allocate space for it instead, 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

static static (when used on global variables/functions) : // example.c int passcode = 12345; // public static int admin_passcode = 67890; // private static (when used on global variables/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

Function static data When used inside a function: static type name = value; declares a static local variable that will be remembered across calls Example: 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, ...