Contents|Index|Previous|Next

Writing Shell Commands with Directory Search

When a dependency is found in another directory through directory search, this cannot change the commands of the rule; they will execute as written. Therefore, you must write the commands with care so that they will look for the dependency in the directory where make finds it.

This is done with the automatic variables such as ‘$ˆ’ (see Automatic Variables).

For instance, the value of ‘$ˆ’ is a list of all the dependencies of the rule, including the names of the directories in which they were found, and the value of ‘$@’ is the target, as in the following example.

foo.o : foo.c
cc -c $(CFLAGS) $ˆ -o $@

The variable CFLAGS exists so you can specify flags for C compilation by implicit rules; we use it here for consistency so it will affect all C compilations uniformly; see Variables Used by Implicit Rules.

Often the dependencies include header files as well, which you do not want to mention in the commands.

The automatic variable ‘$<’ is just the first dependency, as in the following.

VPATH = src:../headers
foo.o : foo.c defs.h
hack.h cc -c $(CFLAGS) $< -o $@