170 likes | 276 Views
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.
E N D
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
Compiling in gcc • GNU C Compiler (gcc) • http://www.gnu.org % gcc reverse.c produces executable in a.out % gcc -o reverse reverse.c
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 supps.c • % gcc -c parts.c • % gcc -c sp.c • % gcc -c db.c • These commands produce supps.o, parts.o, sp.o, db.o • % gcc db.o supps.o parts.o sp.o -o db
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
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
Example Makefile db: db.o supps.o parts.o sp.o gcc db.o supps.o parts.o sp.o -o db db.o: db.c types.h db.h supps.h parts.h sp.h gcc -c db.c supps.o: supps.c types.h supps.h gcc -c supps.c parts.o: parts.c types.h parts.h gcc -c parts.c sp.o: sp.c types.h sp.h gcc -c sp.c
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.
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
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 $<
Simplifying Make Files • So makefile can be simplified as: db: db.o supps.o parts.o sp.o gcc db.o supps.o parts.o sp.o -o db db.o: db.c types.h db.h supps.h parts.h sp.h supps.o: supps.c types.h supps.h parts.o: parts.c types.h parts.h sp.o: sp.c types.h sp.h
Simplifying Make Files • Further simplification (leave out .c files in dependency rules db: db.o supps.o parts.o sp.o gcc db.o supps.o parts.o sp.o -o db db.o: types.h db.h supps.h parts.h sp.h supps.o: types.h supps.h parts.o: types.h parts.h sp.o: types.h sp.h
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
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)
GNU Profiler • Utility gprof profiles a running program • lets you see how the program spends its time • Must use -pg option with gcc • Creates file gmon.out by default
GNU Debug • Utility gdb helps debug a program • Must use -g option when compiling • gdb programname • Get help (help) • Set breakpoints (break line#_or_function) • Look at variables (print variable_name) • Run a program (run, step, or continue after breakpoint) • List a program (list)
Strip Utility • Utility strip removes extra info from a program • Useful after debug / profile • strip programname
Review • Compiling .c files • Including multiple files • Using make to compile several related files • How to simplify an example makefile • touch command