28 GNU make
This is done with the automatic variables such as ‘$^’ (see Section 10.5.3 [Automatic
Variables], page 120). For instance, the value of ‘$^’ is a list of all the prerequisites of the
rule, including the names of the directories in which they were found, and the value of ‘$@’
is the target. Thus:
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 Section 10.3
[Variables Used by Implicit Rules], page 115.)
Often the prerequisites include header files as well, which you do not want to mention
in the recipe. The automatic variable ‘$<’ is just the first prerequisite:
VPATH = src:../headers
foo.o : foo.c defs.h hack.h
cc -c $(CFLAGS) $< -o $@
4.4.5 Directory Search and Implicit Rules
The search through the directories specified in VPATH or with vpath also happens during
consideration of implicit rules (see Chapter 10 [Using Implicit Rules], page 111).
For example, when a file foo.o has no explicit rule, make considers implicit rules, such
as the built-in rule to compile foo.c if that file exists. If such a file is lacking in the current
directory, the appropriate directories are searched for it. If foo.c exists (or is mentioned
in the makefile) in any of the directories, the implicit rule for C compilation is applied.
The recipes of implicit rules normally use automatic variables as a matter of necessity;
consequently they will use the file names found by directory search with no extra effort.
4.4.6 Directory Search for Link Libraries
Directory search applies in a special way to libraries used with the linker. This special
feature comes into play when you write a prerequisite whose name is of the form ‘-lname’.
(You can tell something strange is going on here because the prerequisite is normally the
name of a file, and the file name of a library generally looks like libname.a, not like
‘-lname’.)
When a prerequisite’s name has the form ‘-lname’, make handles it specially by searching
for the file libname.so, and, if it is not found, for the file libname.a in the current directory,
in directories specified by matching vpath search paths and the VPATH search path, and then
in the directories /lib, /usr/lib, and prefix/lib (normally /usr/local/lib, but MS-
DOS/MS-Windows versions of make behave as if prefix is defined to be the root of the
DJGPP installation tree).
For example, if there is a /usr/lib/libcurses.a library on your system (and no
/usr/lib/libcurses.so file), then
foo : foo.c -lcurses
cc $^ -o $@
would cause the command ‘cc foo.c /usr/lib/libcurses.a -o foo’ to be executed when
foo is older than foo.c or than /usr/lib/libcurses.a.
Although the default set of files to be searched for is libname.so and libname.a, this
is customizable via the .LIBPATTERNS variable. Each word in the value of this variable is
Comentarios a estos manuales