Using Wildcards

Up until this point, we've had to explicitly list all of the modules that go into a program or a library. The previous makefile, for example, contained this line:

MODULES = candy.o chips.o licorice.o cookies.o popcorn.o spinach.o

libgoodies.so: $(MODULES)
	$(CXX) -shared $(inputs) -o $(output)

In this case, listing all of the modules that go into libgoodies.so is not such a big deal since there aren't very many of them. But sometimes it can be a real nuisance to list all of the object files, especially if this list is changing rapidly during development. Frequently, you want every single module in the whole directory to be compiled into your program or library. It would be a lot easier if you could just tell makepp to do that without listing them all.

Well, you can. The above lines could be rewritten as:

libgoodies.so: *.o
	$(CXX) -shared $(inputs) -o $(output)

The *.o wildcard matches any existing .o files, or any .o files which do not yet exist but can be made by any of the rules that makepp knows about from any makefiles that it has read. So the wildcard will return the same list of files, no matter whether you haven't compiled anything yet, or whether all the modules have been compiled before.

Of course, if you contaminate your directories with extra files that shouldn't be compiled directly into your library, (e.g., if you write little test programs and leave them in same directory as the library source files), then these modules will be incorrectly included into your library. If you choose to use wildcards, it's up to you to keep the directory clean enough.

Makepp supports the usual unix wildcards and one additional one:

*
Matches any string of 0 or more characters. It will not match the / character. For example, a*c matches ac, abc, and aaaaabc, but not aa/bc.
?
Matches exactly one character (not including /). For example, ???.o matches all filenames that have 3 characters before the .o extension.
[]
Matches any of a list of characters at that position. For example, [abc].o matches a.o, b.o, c.o, but not abc.o or d.o. You can also specify a range of characters, e.g., data_[0-9] will match data_0, data_1, etc.
**
This is a special wildcard, found only in makepp (and the zsh shell, from which I stole the idea). It matches any number of intervening directories. For example, **/*.o matches xyz.o, test_programs/abc.o, and a/deeply/nested/subdirectory/def.o.

If your sources are contained in several subdirectories, and you want to link all the object modules together, you could write it like this:

liboodles.so: **/*.o
	$(CXX) -shared $(inputs) -o $(output)

Tutorial index | Next (functions and variables) | Previous (directories)
Last modified: Fri Aug 25 22:57:09 PDT 2000