Lab 3a: Instructor's Notes


Since chapter 3 in the text is fairly long, we have provided two lab exercises for it: 3a and 3b. In exercise 3a, students build a separately compiled library of metric conversion functions (simple, but useful). In exercise 3b, students learn to build more complicated functions that require selective and repetitive behavior. We have our students do both exercises, 3a one week and 3b the next.

Using a user-defined, separately compiled library with a program changes translation to a multi-step process:

  1. each source file is compiled to create an object file (ending in .o).
  2. The object files are then linked to create a binary executable program.
To coordinate separate compilation, UNIX systems provide the make utility. To use this utility, a programmer prepares a special file called a Makefile (beginning with capital-M by convention, not necessity). The Makefile consists of a series of pairs lines of the form:
   TargetFile: ComponentFile1 ComponentFile2 ... ComponentFileN
	   Command
where Command is a UNIX command that makes TargetFile from the N ComponentFiles. Command must be preceded by a TAB and followed by a RETURN (or ENTER).

To illustrate, our binary executable program driver is made by linking the object files driver.o and Metric.o, and so the first pair in the Makefile is roughly as follows:

   driver: driver.o Metric.o
	   g++ driver.o Metric.o -o driver
Similarly, driver.o is made by compiling driver.cppMetric.h, and so the second pair in the Makefile is:
   driver.o: driver.cpp Metric.h
	   g++ -c driver.cpp
The -c switch tells g++ to just compile driver.cpp and create an object file, instead of compiling and linking to create a binary executable (the default action). A similar rule specifies the making of Metric.o.

We provide a partial Makefile that can be accessed from the exercise. The sections of the exercise starting with Multi-File Translation teach the students about Makefile structure and how to complete it.

We also provide "starter" files in the exercise for each of the other components in the project: driver.cpp, metric.h, metric.cpp, and metric.doc. If you wish to avoid network contention and traffic, save all of these files ahead of time in a 3a subdirectory of your course directory, and instruct your students to copy them from there. (Note: Our browser replaced the TAB character before each g++ with spaces. If yours does the same, you will need to delete those spaces and replace them with a TAB.)


Back to This Lab's Home Page

Forward to the Prelab Questions


Copyright 1998 by Joel C. Adams. All rights reserved.