next up previous
Next: Lots of tips Up: A sample makefile and Previous: Special macros

Suffix rules


Most of the commands you use to update files are similar; to build most C++ programs, you call g++ with similar flags and really just change the name of the file you're compiling.

Make allows you to define suffix rules. In the sample, there's one which tells it how to build a .o file from a .cc file:

   ->   $(CXX) $(CXXFLAGS) -c $<

With suffix rules, you can leave out the command part of a dependency rule, for example:

usepix.o: usepix.h pixmap.h

If make decides to update usepix.o, it sees from this line that it depends on, and it sees from the .cc.o rule that to update it, it should run g++ using the flags defined in CXXFLAGS to compile In fact, the command it executes turns out to be:

$(CXX)  $(CXXFLAGS)................................. -c $<
g++     -g -I. -I/home/garrett/Projects/Tapestry/lib -c

In fact, make is so smart that you don't even need to tell it what usepix.o depends on (although it is a good idea). Once it figures out that the file exists, it assumes that usepix.o depends upon itgif and applies the suffix rule accordingly.

Make also has a number of built in suffix rules, which are roughly as follows:

   ->   $(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

   ->   $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<

.o :
   ->   $(CC) $(LDFLAGS) $^ -o $@ $(LOADLIBES)

The .SUFFIXES: .cc line tells make to apply suffix rules only to .cc files and is not absolutely necessary.

Fri Jan 24 17:04:25 EST 1997