继续翻译
5.7 Recursive Use of `make' =========================== Recursive use of `make' means using `make' as a command in a makefile. This technique is useful when you want separate makefiles for various subsystems that compose a larger system. For example, suppose you have a subdirectory `subdir' which has its own makefile, and you would like the containing directory's makefile to run `make' on the subdirectory. You can do it by writing this: subsystem: cd subdir && $(MAKE) or, equivalently, this (*note Summary of Options: Options Summary.): subsystem: $(MAKE) -C subdir You can write recursive `make' commands just by copying this example, but there are many things to know about how they work and why, and about how the sub-`make' relates to the top-level `make'. You may also find it useful to declare targets that invoke recursive `make' commands as `.PHONY' (for more discussion on when this is useful, see *note Phony Targets::). For your convenience, when GNU `make' starts (after it has processed any `-C' options) it sets the variable `CURDIR' to the pathname of the current working directory. This value is never touched by `make' again: in particular note that if you include files from other directories the value of `CURDIR' does not change. The value has the same precedence it would have if it were set in the makefile (by default, an environment variable `CURDIR' will not override this value). Note that setting this variable has no impact on the operation of `make' (it does not cause `make' to change its working directory, for example).
5.7 make 的递归使用
===========================
递归是使用 make 意味着在 makefile中把make 当作命令使用。这个技术在你想要一个大系统的每个子系统都有一个单独的makefile时候有用。例如,设想你有一个子目录 subdir ,有自己的makefile, 并且你想要包含此makefile的目录上的makefile来运行,你可以这样写:
subsystem:
cd subdir && $(MAKE)
或者,等价地,这样写(*note Summary of Options: Options Summary.):
subsystem:
$(MAKE) -C subdir
你可以用拷贝这个例子的方式来写自己的递归make 命令,但是也有很多如它们如何工作和为何如此工作以及子make如何和顶级make关联的东西需要知道。你也可以发现,声明激活递归式make指令为.PHONY很有用(若要参考更多相关信息,参见 *note Phony Targets::)。
为了方便起见,当 GNU make 启动(当它处理了任何的 -C 选项),它设置了变量 CURDIR 到当前工作目录。make 不会再改变其值:特别是请注意,如果你从其他目录包含了文件,CURDIR 的值是不会改变的。如果是设置在makefile,优先级别也是一样。
(缺省地,环境变量 CURDIR 无法覆盖此值)
请注意设置这个值对make 的操作没有影响(它不会导致make 改变当前工作目录)。
后文待续
本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/10/01/2709685.html,如需转载请自行联系原作者