GNU make manual 翻译(八十一)

简介:
复制代码
   Consider an example where your targets are to be placed in a separate directory, and that directory might not exist before `make' is run.  Inthis situation, you want the directory to be created before any targets are placed into it but, because the timestamps on directories change whenever a file is added, removed, or renamed, we certainly don't want to rebuild all the targets whenever the directory's timestamp changes.
   One way to manage this is with order-only prerequisites: make the 
directory an order-only prerequisite on all the targets:                        
                        
     OBJDIR := objdir                        
     OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)                        
                        
     $(OBJDIR)/%.o : %.c                        
             $(COMPILE.c) $(OUTPUT_OPTION) $<                        
                        
     all: $(OBJS)                        
                        
     $(OBJS): | $(OBJDIR)                        
                        
     $(OBJDIR):                        
             mkdir $(OBJDIR)                        
                        
   Now the rule to create the `objdir' directory will be run, if needed, before any `.o' is built, but no `.o' will be built because the `objdir' directory timestamp changed. 
复制代码
 

考虑一个例子,你的目的可以放置到一个独立的目录,这个目录可能在make 运行时尚不存在。在这种场合下,你可能在任何目的被放入此目录前去创建它们,但是由于目录上的时间戳会因文件的增加移动或删除而变化,我们肯定不想在目录时间戳发生变化时去重新建立所有目的文件。

管理此种问题的一个办法就是使用 order-only 前提条件: 使此目录成为所有目的的前提条件:

OBJDIR := objdir 
OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)

$(OBJDIR)/%.o : %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<

all: $(OBJS)

$(OBJS): | $(OBJDIR)

$(OBJDIR):
mkdir $(OBJDIR)

现在如果有需要,在任何.o 文件被构建之前,创建 objdir 的规则会运行。但是目录时间戳的改变不会再导致 .o 文件的重新创建。

后文待续


本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/19/2692672.html,如需转载请自行联系原作者
目录
相关文章
|
Linux C语言
make: gcc:命令未找到
make: gcc:命令未找到
make: gcc:命令未找到
|
C语言 C++ Perl
如何处理错误消息Please install the gcc make perl packages
如何处理错误消息Please install the gcc make perl packages
143 0