Computer Science 2611
Summer 2000
Laboratory Assignment 7

Handed out: 7/6/00

The assignment

Large programs are broken into many files. This assignment is to break up your solution to assignment 6 into separate files. Put the random number generator in an implementation file called random.cc or random.cpp, and a header file called random.h. Put the part of the program that computes an estimate of pi in a file called pi.cc or pi.cpp. There should be a total of three files. Turn in a printout of all three.

Header and implementation files

Each part of a C++ program is called a module. Each module normally consists of two files, a header file and an implementation file. The header file is an advertisement to the other parts of the program, telling what functions and other things this module provides. The implementation file provides the details of how the functions are performed. It is in the implementation file where the things that are provided are created.

To make a class a separate module, put the class into the header file. Do not put the definitions of the functions in the class. Put the function definitions into the implementation file.

Including a header file

To include a header file in another program, use a #include directive. Put quotes around the name of the header file. (The quotes tell the compiler to look in the current directory for the header file instead of in the directory where the standard library header files are located.) To include header file random.h, you write
  #include "random.h"
Use this line in any file that needs to use the things described in random.h. Important: Be sure to include random.h in the implementation file random.cc or random.cpp. Each implementation module always includes its own header file.

Compiling and linking

When a program is broken into separate modules, each module is compiled separately. The compiled versions are linked together. With Unix, use a makefile to do this. Create a file called Makefile, and put the following in it. I am presuming that your two implementation files are called pi.cc and random.cc, and that you want your executable file to be called pi.
  COMPILE = g++ -c -Wall -O -Wshadow
  LINK    = g++

  pi: pi.o random.o
	$(LINK) -o pi random.o pi.o

  pi.o: pi.cc random.h
	$(COMPILE) pi.cc

  random.o: random.cc random.h
	$(COMPILE) random.cc

  clean:
	rm random.o pi.o pi
The lines that define COMPILE and LINK tell what $(COMPILE) and $(LINK) are replaced by. Compiling with option -c creates a file that is not executable, but that is prepared to link to other files. For example, running command
   g++ -c random.cc
creates file random.o. You cannot run random.o, but you can link it into another program. The g++ compiler will link for you, as you can see in the makefile.

Each item that you build (pi, pi.o, random.o) is a target in the makefile. The target is followed by a colon, and then a list of the files that are used in building it. After that is a command (or several commands) with a tab in front of it (or in front of each one, if there are several). The command is used to build the target. It is very important to begin each command line with at tab.

To build a target t, type command

  make t
You can build the first target in the makefile by just typing command
  make
Normally, you put the target that you most often build first, so that you can use this abbreviated command.

The target clean has been defined so that you can type

  make clean
to remove all of the machine-generated files. If you move from one kind of computer to another, for example, you probably want to make your implementation clean and recompile.