Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Software Development CS240 Dick Steflik. Make “make” is a software engineering tool for managing and maintaining computer programs  Help minimize the.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries.
Guide To UNIX Using Linux Third Edition
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
Computer Science 210 Computer Organization Introduction to C.
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Programming With C.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Computer Science 210 Computer Organization Building an Assembler Part IV: The Second Pass, Syntax Analysis, and Code Generation.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
 Prepared by: Eng. Maryam Adel Abdel-Hady
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
CS 153: Concepts of Compiler Design August 29 Class Meeting
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Introduction to C Topics Compilation Using the gcc Compiler
CS1010 Programming Methodology
Computer Science 210 Computer Organization
C Basics.
CS1010 Programming Methodology
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
C Preprocessor(CPP).
SCMP Software Development Spring 2018 James Skon
Writing Large Programs
Main() { int fact; fact = Factorial(4); } main fact.
SCMP Software Development Spring 2018 James Skon
CMPE 152: Compiler Design August 27 Class Meeting
Presentation transcript:

Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation

Program Decomposition C programs consist of lots of functions Organize these and save them in libraries (like Python modules) Separately compile them for distribution and link them to client applications

Example: An Assembler Source program listing, error messages (file and/or terminal) Text file Assembler Object file Sym file

Decompose into Modules Scanner Parser Source program listing, error messages (file and/or terminal) Object file Text file CharacterIO Character stream Token stream Symbol tableOpcode table Sym file Tools CharacterIO – handles files, text Scanner – recognizes numbers, symbols, assembler directives Parser – handles syntax checking, code generation

/* This program prints the factorial of 6. */ #include int factorial(int n); // Prototype shows the function specs int main(){ printf("The factorial of 6 is %d\n", factorial(6)); } int factorial(int n){ if (n == 1) return 1; else return n * factorial(n - 1); } Using a Function Prototype A function prototype is a header that appears early, to aid the human reader of the code (like a Java interface, sort of)

Where Should Functions Go? We can put ‘em all in one file with the application; prototypes are at the beginning of the file, and we work top down But this requires recompiling the whole file after one small change Work can’t be divided up among many developers Functions can’t be maintained independently of their clients

Create a Library of Functions Place the function prototypes for a module in a header file (.h extension) Place the completed function definitions for that module in an implementation file (.c extension) Compile the c. file, link it to a test driver, test it, and store it away Example: a mylib library with factorial and gcd

Structure of the Program usemylib.c mylib.c mylib.h Compile time Link time Compile time Header file Implementation file Main function file mylib.c and usemylib.c can be compiled in separate steps, by different people, in different organizations They each rely on the interface in mylib.h

/* Header file for factorial and gcd functions. */ int factorial(int n); // Prototype for factorial function int gcd(int a, int b); // Prototype for gcd function The Header File mylib.h The header file contains function prototypes and may also contain #define macros

/* Implementation file for factorial and gcd functions. */ #include "mylib.h" int factorial(int n){ if (n == 1) return 1; else return n * factorial(n - 1); } int gcd(int a, int b){ if (b == 0) return a; else return gcd(b, a % b); } The Implementation File mylib.c The implementation file also includes its header file (for consistency checking) – note the quotes rather than the

/* Test program uses both mylib functions. */ #include #include "mylib.h" int main(){ printf("The factorial of 6 is %d\n", factorial(6)); printf("The gcd of 9 and 15 is %d\n", gcd(15, 9)); } The Test Driver File usemylib.c

Separate Compilation and Linking Save each implementation file with a.c extension Work bottom up, compile each.c file with gcc –c.c If no errors, link the object files with gcc –o.o....o If no errors, run the program with./

make and Makefiles Many modules to keep track of Which ones are up to date, which ones need to be recompiled? The UNIX make tool and makefiles help to manage this problem

make and Makefiles When you run make, UNIX looks for a file named makefile or Makefile in the current working directory A makefile contains info about the dependencies among program modules and commands to compile and/or link them

Makefile Entries targets : prerequisites commands This is the format of an entry. There are one or more entries in a makefile. A tab must go here Targets are dependent on prerequisites Targets and prerequisites are file names Commands are similar to those we’ve seen already

A Makefile for Compiling mylib mylib.o : mylib.c mylib.h gcc –c mylib.c target prerequisites command

Add Another Entry for usemylib usemylib.o : usemylib.c mylib.h gcc –c usemylib.c mylib.o : mylib.c mylib.h gcc –c mylib.c

Add Another Entry to Link the Program usemylib : usemylib.o mylib.o gcc –o usemylib usemylib.o mylib.o usemylib.o : usemylib.c mylib.h gcc –c usemylib.c mylib.o : mylib.c mylib.h gcc –c mylib.c

How make works > make > make mylib.o The first call compares the time stamps of the targets and prerequisites and runs only those commands that “make” the targets up to date The second call runs only the command whose target is the argument mylib.o