Unformatted text preview:

1 1 Building!2 Goals of this Lecture!• Help you learn about:"• The build process for multi-file programs"• Partial builds of multi-file programs"• make, a popular tool for automating (partial) builds"• Why?"• A complete build of a large multi-file program typically consumes many hours"• To save build time, a power programmer knows how to do partial builds"• A power programmer knows how to automate (partial) builds using make2 3 Example of a Three-File Program!• Program divided into three files"• intmath.h: interface, included into intmath.c and testintmath.c • intmath.c: implementation of math functions"• testintmath.c: implementation of tests of the math functions"• Recall the program preparation process"• testintmath.c and intmath.c are preprocessed, compiled, and assembled separately to produce testintmath.o and intmath.o • Then testintmath.o and intmath.o are linked together (with object code from libraries) to produce testintmath"4 Motivation for Make (Part 1)!• Building testintmath, approach 1:"• Use one gcc217 command to preprocess, compile, assemble, and link"intmath.h intmath.c testintmath.c testintmath gcc217 testintmath.c intmath.c –o testintmath Thatʼs not how itʼs done in the real world…"3 5 gcc217 –c intmath.c gcc217 –c testintmath.c Motivation for Make (Part 2)!• Building testintmath, approach 2:"• Preprocess, compile, assemble to produce .o files"• Link to produce executable binary file"Thatʼs how itʼs done in the real world; Why?..."Recall: -c option"tells gcc217 to omit link"intmath.h intmath.c testintmath.c testintmath testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath 6 Partial Builds!• Approach 2 allows for partial builds"• Example: Change intmath.c • Must rebuild intmath.o and testintmath • Need not rebuild testintmath.o!!! intmath.h intmath.c testintmath.c testintmath testintmath.o intmath.o gcc217 –c testintmath.c gcc217 –c intmath.c changed"gcc217 testintmath.o intmath.o –o testintmath4 7 Partial Builds (cont.)!• Example: Change testintmath.c • Must rebuild testintmath.o and testintmath • Need not rebuild intmath.o!!! If program contains many .c files, could save many hours of build time"intmath.h intmath.c testintmath.c testintmath testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.c gcc217 –c intmath.c changed"8 Partial Builds (cont.)!• However, changing a .h file can be more dramatic"• Example: Change intmath.h • intmath.h is #included into testintmath.c and intmath.c • Changing intmath.h effectively changes testintmath.c and intmath.c • Must rebuild testintmath.o, intmath.o, and testintmath intmath.h intmath.c testintmath.c testintmath testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.c gcc217 –c intmath.c changed"5 9 Wouldnʼt It Be Nice…!• Observation"• Doing partial builds manually is tedious and error-prone"• Wouldnʼt it be nice if there were a tool"• How would the tool work?"• Input:"• Dependency graph (as shown previously)"• Specifies file dependencies"• Specifies commands to build each file from its dependents"• Date/time stamps of files"• Algorithm:"• If file B depends on A and date/time stamp of A is newer than date/time stamp of B, then rebuild B using the specified command"• Thatʼs make!"10 Make Fundamentals!• Command syntax"make [-f makefile] [target] • makefile • Textual representation of dependency graph"• Contains dependency rules!• Default name is makefile, then Makefile"• target • What make should build"• Usually: .o file, or an executable binary file"• Default is first one defined in makefile6 11 Dependency Rules!• Dependency rule syntax"target: dependencies <tab>command • target: the file you want to build"• dependencies: the files on which the target depends"• command: what to execute to create the target (after a TAB character)"• Dependency rule semantics"• Build target iff it is older than any of its dependencies • Use command to do the build"• Work recursively; examples illustrate…"12 Makefile Version 1!testintmath: testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath testintmath.o: testintmath.c intmath.h gcc217 -c testintmath.c intmath.o: intmath.c intmath.h gcc217 -c intmath.c intmath.h intmath.c testintmath.c testintmath testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.c gcc217 –c intmath.c Makefile:"Three dependency rules; each captures a fragment of the graph"7 13 Version 1 in Action!$ make testintmath gcc217 -c testintmath.c gcc217 -c intmath.c gcc217 testintmath.o intmath.o -o testintmath $ touch intmath.c $ make testintmath gcc217 -c intmath.c gcc217 testintmath.o intmath.o -o testintmath $ make testintmath make: `testintmath' is up to date. $ make make: `testintmath' is up to date. At first, to build testintmath"make issues all three gcc "commands"Use the touch command to"change the date/time stamp"of intmath.c"make does a partial build"make notes that the specified"target is up to date"The default target is testintmath,"the target of the first dependency rule"14 Non-File Targets!• Adding useful shortcuts for the programmer"• make all: create the final binary"• make clean: delete all binaries"• make clobber: delete all temp files, core files, binaries, etc."• Commands in the example"• rm –f: remove files without querying the user"• Files ending in ʻ~ʼ and starting/ending in ʻ#ʼ are Emacs backup files"• core is a file produced when a program “dumps core”"all: testintmath clean: rm -f testintmath *.o clobber: clean rm -f *~ \#*\# core8 15 Makefile Version 2!# Dependency rules for non-file targets all: testintmath clobber: clean rm -f *~ \#*\# core clean: rm -f testintmath *.o # Dependency rules for file targets testintmath: testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath testintmath.o: testintmath.c intmath.h gcc217 -c testintmath.c intmath.o: intmath.c intmath.h gcc217 -c intmath.c 16 Version 2 in Action!$ make clean rm -f testintmath *.o $ make clobber rm -f testintmath *.o rm -f *~ \#*\# core $ make all


View Full Document

Princeton COS 217 - Building

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Building
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Building and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Building 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?