GNU make manual 翻译(九十四)

继续翻译

4.5.4 Writing Recipes with Directory Search
-------------------------------------------
When a prerequisite is found in another directory through directory
search, this cannot change the recipe of the rule; they will execute as
written.  Therefore, you must write the recipe with care so that it
will look for the prerequisite in the directory where `make' finds it.

   This is done with the "automatic variables" such as `$^' (*note
Automatic Variables::).  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; *note Variables Used by Implicit
Rules: Implicit Variables.)

   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.5.4 写目录搜索相关的片段
-------------------------------------------

当一个前提条件通过目录搜索在另一个目录中被发现,这并不会改变规则的片段;片段将按照所写的被执行。因此,你必须仔细地书写片段这样它可以在make找到的目录中搜索前提条件。

这是用 类似$^ 之类的自动变量来实现的(*note Automatic Variables::)。例如,$^的值是规则中所有前提条件的列表,其中也包括它们所在目录的名字,而$@是目的的名字。因此:

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

(变量 CFLAGS 已存在,所以你可以用隐式规则来为C编译指定此标志;我们为一贯性考虑而是用它,所以它将影响到所有的C 编译; *note Variables Used by Implicit Rules: Implicit Variables)

通常,前提条件中也包括头文件,你可能不想在片段中提及此事。自动变量 $< 指代第一个前提条件:

VPATH = src:../headers

foo.o : foo.c defs.h hack.h
cc -c $(CFLAGS) $< -o $@

后文待续

posted @ 2012-09-20 07:58  健哥的数据花园  阅读(260)  评论(0编辑  收藏  举报