1 / 15

Chapter 12 C Programming Tools

Graham Glass and King Ables, UNIX for Programmers and Users , Third Edition, Pearson Prentice Hall, 2003. Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks. Chapter 12 C Programming Tools. Compiling in gcc. GNU C Compiler (gcc) http://www.gnu.org.

dinos
Télécharger la présentation

Chapter 12 C Programming Tools

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. Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks Chapter 12C Programming Tools cs3320

  2. Compiling in gcc • GNU C Compiler (gcc) • http://www.gnu.org % gcc reverse.c produces executable in a.out % gcc -o reverse reverse.c cs3320

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

  4. cs3320

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

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

  7. Example Makefile /* REVERSE.H */ void reverse (); /* Declare but do not define this function */ /* REVERSE.C */ #include <stdio.h> #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 */ } /* MAIN1.C */ #include <stdio.h> #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); } #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 cs3320

  8. 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. 1 7 main1 3 2 main1.o reverse.o 6 5 6 7 4 5 reverse.c reverse.h reverse.h 4 main1.c 3 1 2 cs3320

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

  10. 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 $< cs3320

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

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

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

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

  15. Review • Compiling .c files • Including multiple files • Using make to compile several related files • How to simplify an example makefile • touch command cs3320

More Related