cs33201 Chapter 12 C Programming Tools Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks
cs33202 Compiling in gcc % gcc reverse.c produces executable in a.out % gcc -o reverse reverse.c GNU C Compiler (gcc) –
cs33203 Multi-module Program types.h supps.h, supps.c parts.h, parts.c sp.h, sp.c db.h, db.c (this has the main() function) Compile these programs separately; –% gcc -c reverse.c –% gcc -c main1.c These commands produce reverse.o, main1.o % gcc reverse.o main1.o -o main1
cs33204
cs33205 Unix File-Dependency System: make % make -f fileName Makefiles: consist of a list of interdependency rules of the form: targetList:dependencyList commandList Rules must be separated by at least one line
cs33206 Unix File-Dependency System: make targetList is a list of target files dependencyList is a list of files on which the files in targetList depend on commandList is a list of zero or more commands, separated by new lines, that reconstruct the target files from the dependency files NOTE: Each line in commandList MUST start with a tab
cs33207 Example Makefile #MAKEFILE main1: main1.o reverse.o cc main1.o reverse.o -o main1 main1.o: main1.c reverse.h cc -c main1.c reverse.o: reverse.c reverse.h cc -c reverse.c /* REVERSE.C */ #include #include "reverse.h" void reverse (before, after) char *before; /* A pointer to the original string */ char *after; /* A pointer to the reversed string */ { int i; int j; int len; len = strlen (before); for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */ after[i] = before[j]; after[len] = NULL; /* NULL terminate reversed string */ } /* REVERSE.H */ void reverse (); /* Declare but do not define this function */ /* MAIN1.C */ #include #include "reverse.h"/* Contains the prototype of reverse () */ main (){ char str [100]; reverse ("cat", str); /* Invoke external function */ printf ("reverse (\"cat\") = %s\n", str); reverse ("noon", str); /* Invoke external function */ printf ("reverse (\"noon\") = %s\n", str); }
cs33208 Order of Make rules The order of make rules is important. The make utility starts from the first rule and creates a tree with target files at the root and the dependency files as children. main1 main1.oreverse.o main1.c reverse.h reverse.creverse.h
cs33209 Order of Make rules The make utility then works up the tree –From the leaf nodes to the root node –Looking to see if the last modification time of each node is more recent than the last modification time of the immediate parent node –If so, the associated parent's rule is executed
cs Make Rules Make Rules of the form: xxx.o: reverse.c reverse.h gcc -c xxx.c where xxx varies from each rule Pre-defined rule (how to make.o from.c files):.c.o: /bin/cc -c -O $<
cs Simplifying Make Files So makefile can be simplified as: $ cat main2.make main2: main2.o reverse.o palindrome.o cc 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
cs Simplifying Make Files Further simplification (leave out.c files in dependency rules $ cat main2.make1 main2: main2.o reverse.o palindrome.o cc main2.o reverse.o palindrome.o -o main2 main2.o: palindrome.h reverse.o: reverse.h palindrome.o: palindrome.h reverse.h
cs Touch Utility touch -c {fileName}+ –updates the last modification and access times of the named files to the current time. By default, if a specified file does not exist it is created with zero size. –To prevent this default, use -c option. Use touch to force make to recompile files
cs Archiving Modules GNU archive utility: ar ar key archivename {file}* Creates archive files (.a) –Add (r, if file is not there) –Remove (d) –Replace (r) –Append (q) adds to end of archive –Table of contents (t) –Extract and copy archive content to current directory (x) –Verbose (v) $ ar r string.a reverse.o palindrome.o $ ar t string.a $ ar d string.a reverse.o $ ar q string.a reverse.o $ ar x string.a.
cs Review Compiling.c files Including multiple files Using make to compile several related files How to simplify an example makefile touch command