SEG 4570 System Design and Implementation Tutorial 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

SEG 4570 System Design and Implementation Tutorial 2

Description:

e.g. a program which reverses a string (reverse.c) components of reverse.c ... Palindrome is a string that reads the same forward and backward. e.g. 'txt' is a ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 26
Provided by: sepC5
Category:

less

Transcript and Presenter's Notes

Title: SEG 4570 System Design and Implementation Tutorial 2


1
SEG 4570System Design and ImplementationTutoria
l 2
2
Tutorial Content
  • Program Development in Unix
  • Single Module vs Multiple Modules
  • Separate Compilation
  • Linking Object Modules
  • Unix File Dependency System Make
  • Unix Archive System Ar

3
Program Development in Unix
  • Single-Module Program
  • e.g. a program which reverses a string
    (reverse.c)
  • components of reverse.c
  • 1. main (call a function to reverse a string)
  • 2. A function reverses the input string
  • e.g. reverse (char before, char after)
  • / function definition /
  • compile gcc reverse.c -o reverse

4
Program Development in Unix
  • main problems for single-module program
  • 1) The reverse function cannot be used in other
    programs!
  • 2) Difficult to debug such a large program!
  • Multi-Module Program
  • share the reverse ( ) function for other
    programs
  • how to make it?
  • 1. Compile the function separately (easier to
    debug)
  • 2. Link the resultant object modules into the
    program want to use it. (make programs reusable)

5
Program Development in Unix
  • Preparing a Reusable Function
  • Two components
  • 1. A header file that contains the function
    prototype. (declaration of the function)
  • e.g. reverse.h
  • / reverse.h/
  • void reverse ( char, char )
  • / Declaration only, no definition /

6
Program Development in Unix
  • 2. A source code module contains the source codes
    of the function (definition of the function)
  • e.g. reverse.c
  • / reverse.c /
  • include
  • include reverse.h
  • void reverse (char before, char after)
  • / definition of the function /

7
Program Development in Unix
  • How to use reverse ( )
  • e.g. main1.c
  • / main1.c /
  • include
  • include reverse.h
  • main ( )
  • char str100
  • reverse(cat, str)

8
Program Development in Unix
  • Separately Compiling and Linking Modules
  • 1. Separate Compilation
  • gcc -c reverse.c main1.c
  • This generates two object modules
  • 1) reverse.o, 2) main1.o
  • 2. Link Object Modules
  • gcc reverse.o main1.o -o reverse
  • This generates an executable file reverse

9
Program Development in Unix
  • Reusing the Reverse Function
  • if want to write a program to check whether a
    string is a palindrome or not, we can do as
    follow.
  • Palindrome is a string that reads the same
    forward and backward. e.g. txt is a
    palindrome.
  • 1. Prepare palindrome.h
  • / palindrome.h /
  • int palindrome ( char )

10
Program Development in Unix
  • 2. Prepare palindrome.c
  • / palindrome.c /
  • include
  • include
  • include palindrome.h
  • include reverse.h
  • int palindrome (char str)
  • char rev_str100
  • reverse(str, rev_str)
  • return(strcmp (str, rev_str) 0)

11
Program Development in Unix
  • 3. Prepare a program to use palindrome ( )
  • e.g. main2.c
  • / main2.c /
  • include
  • include palindrome.h/ need not include
    reverse.h /
  • main ( )
  • ... palindrome(cat) ...

12
Program Development in Unix
  • Maintaining Multi-module Programs
  • Q1. What ensures that object modules and
    executables are kept up to date?
  • Q2. What stores the object modules?
  • A1. make, the Unix file dependency system
  • A2. ar, the Unix archive system

13
Unix File Dependency System
  • Why use make
  • Without make, if reverse.c is changed, we have
    to
  • 1) Recompile reverse.c
  • 2) Link reverse.o and main1.o new main1
  • 3) Link reverse.o and main2.o new main2
  • How about for programs with hundreds of object
    files?
  • We can create a makefile for each executable file
    that contains a list of all of its file
    interdependencies to easy the compilation
    process..

14
Unix File Dependency System
  • Utility
  • make -f makefile
  • Make Rules Format
  • targetList dependencyList
  • / tab / commandList
  • targetList list of target files
  • dependencyList list of files that the files in
    targetList depend on
  • commandList list of commands

15
Unix File Dependency System
  • Makefile for main1
  • E.g. main1.make1
  • main1 main1.c reverse.c reverse.h
  • gcc -g main1.c reverse.c -o main1
  • Makefile for main1
  • e.g. main1.make2
  • main1 main1.o reverse.o
  • gcc main1.o reverse.o -o main1
  • main1.o main1.c reverse.h
  • gcc -c main1.c
  • reverse.o reverse.c reverse.h
  • gcc -c reverse.c

16
Unix File Dependency System
  • The order of make rules is important. A tree of
    interdependencies is created in make system.
  • () make ordering

17
Unix File Dependency System
  • How to ensure object modules and executables are
    kept up-to-date?
  • The make system will work on leave node first
    (see previous page). It can ensure all the object
    modules and executables are up-to-date by
    checking the timestamp of their dependency lists.
    If the object module or executable is more
    up-to-date than all the files in its dependency
    list, then it need not be compiled again.
  • e.g. if main1.o is more up-to-date than both
    main1.c and reverse.h, then it will not be
    compiled again.

18
Unix File Dependency System
  • Makefile for main2 (main2.make)
  • main2 main2.o reverse.o palindrome.o
  • gcc main2.o reverse.o palindrome.o -o main2
  • main2.o main2.c palindrome.h
  • gcc -c main2.c
  • reverse.o reverse.c reverse.h
  • gcc -c reverse.c
  • palindrome.o palindrome.c palindrome.h
    reverse.h
  • gcc -c palindrome.c

19
Unix File Dependency System
  • Simpler makefiles
  • predefined rule
  • .c.o
  • /bin/cc -c
  • main2.make1
  • main2 main2.o reverse.o palindrome.o
  • gcc main2.o reverse.o palindrome.o -o main2
  • main2.o main2.c palindrome.h
  • reverse.o reverse.c reverse.h
  • palindrome.o palindrome.c palindrome.h
    reverse.h

20
Unix File Dependency System
  • Simpler makefiles
  • since xxx.o is always dependent on xxx.c,
    main2.make1 can be changed to
  • main2.make2
  • main2 main2.o reverse.o palindrome.o
  • gcc main2.o reverse.o palindrome.o -o main2
  • main2.o palindrome.h
  • reverse.o reverse.h
  • palindrome.o palindrome.h reverse.h

21
Unix File Dependency System
  • A general makefile with Macros
  • CC gcc
  • CCOPT -O2
  • LIB -lm
  • INC -I./sub
  • OBJS obj1.o obj2.o objn.o
  • TARGET obj_file
  • .c.o .h .c
  • (CC) (CCOPT) (INC) -c .c
  • (TARGET) (OBJS)
  • (CC) (LIB) -o (TARGET) (OBJS)

22
Unix Archive System
  • Why use Ar?
  • For large C project, thousands of object modules
    are used. Ar is used to group and organize these
    object modules.
  • Utility
  • ar option archiveName fileName
  • archiveName .a
  • option d, delete a file from an archive
  • q, append a file to an archive, no matter
    present or not
  • r, adds if not present, replaces if present
  • t, displays the table of content of an
    archive
  • x, copies a list of files from an archive to
    current directory

23
Unix Archive System
  • How to use archive files?
  • It can be accessed by compiler and linker by
    simply supplying the archive name as an argument.

  • Examples
  • To create an archive file named string.a
  • ar r string.a reverse.o palindrome.o
  • To list the object modules in string.a
  • ar t string.a
  • To link main.o with string.a
  • gcc main.o string.a -o main2

24
Unix Archive System
  • To delete reverse.o from string.a
  • ar d string.a reverse.o
  • To add reverse.o to string.a
  • ar q string.a reverse.o or
  • ar r string.a reverse.o
  • After creating the archive, reverse.o and
    palindrome.o are stored in string.a and they can
    be deleted to save disk space.
  • To copy reverse.o and palindrome.o back
  • ar x string.a reverse.o

25
Unix Archive System
  • To maintain an archive using Make system
  • Using ( ) to refer object module in archive
  • main2 main2.o string.a(reverse.o)
    string.a(palindrome.o)
  • gcc main2.o string.a -o main2
  • main2.o palindrome.h
  • string.a(reverse.o) reverse.h
  • string.a(palindrome.o) palindrome.h reverse.h
Write a Comment
User Comments (0)
About PowerShow.com