1 / 19

Computer Science 210 Computer Organization

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)

glora
Télécharger la présentation

Computer Science 210 Computer Organization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

  8. Structure of the Program Header file Main function file mylib.h usemylib.c Compile time Compile time Link time Implementation file mylib.c 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

  9. The Header File 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 contains function prototypes and may also contain #define macros

  10. The Implementation File mylib.c /* 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); } intgcd(int a, int b){ if (b == 0) return a; else return gcd(b, a % b); } The implementation file also includes its header file (for consistency checking) – note the quotes rather than the < >

  11. The Test Driver File usemylib.c /* Test program uses both mylib functions. */ #include <stdio.h> #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)); }

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

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

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

  15. Makefile Entries This is the format of an entry. There are one or more entries in a makefile. targets : prerequisites commands 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

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

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

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

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

More Related