且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GNU make manual 翻译( 一百二十五)

更新时间:2022-09-21 17:52:15

继续翻译

GNU make manual 翻译( 一百二十五)
   Here is the pattern rule to generate a file of prerequisites (i.e.,
a makefile) called `NAME.d' from a C source file called `NAME.c':

     %.d: %.c
             @set -e; rm -f $@; \
              $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
              sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
              rm -f $@.$$$$

*Note Pattern Rules::, for information on defining pattern rules.  The
`-e' flag to the shell causes it to exit immediately if the `$(CC)'
command (or any other command) fails (exits with a nonzero status).  

   With the GNU C compiler, you may wish to use the `-MM' flag instead
of `-M'.  This omits prerequisites on system header files.  *Note
Options Controlling the Preprocessor: (gcc.info)Preprocessor Options,
for details.

   The purpose of the `sed' command is to translate (for example):

     main.o : main.c defs.h

into:

     main.o main.d : main.c defs.h

This makes each `.d' file depend on all the source and header files
that the corresponding `.o' file depends on.  `make' then knows it must
regenerate the prerequisites whenever any of the source or header files
changes.
GNU make manual 翻译( 一百二十五)

下面是一个模式规则,用来生成一个前提条件文件(例如一个 makefile) ,此文件名为 NAME.d ,是从名为 NAME.c 的源文件而来:

%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

关于 定义 模式规则,参见 *Note Pattern Rules::。

-e 标志是的如果 $(CC) (或其他指令)一旦失败(以非0值退出),shell 立即退出。 

 

对GNU C 编译器而言,你可期望用 -MM 标志来代替 -M 标志,这省略了系统头文件部分的前提条件。 细节参见 *Note Options Controlling the Preprocessor: (gcc.info) Preprocessor Options.

The purpose of the `sed' command is to translate (for example):

 

sed 命令的目的是翻译(例如):

把 main.o : main.c defs.h

翻译成:

main.o main.d : main.c defs.h

这使得 每个 .d 文件依赖于所有的.o 文件依赖的 源文件和头文件。make 这样就会知道,当源文件或者头文件变化时,它必须重新生成前提条件。

后文待续





本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/26/2704262.html,如需转载请自行联系原作者