【发布时间】:2017-06-30 23:31:55
【问题描述】:
我有一个包含多个子目录的项目,主 Makefile 需要能够构建/清理一些子目录 - 子目录的确切名称在变量中传递(示例中为 DIRS):
DIRS = dir1 dir2 # Usually passed from the command line
.PHONY: all clean $(DIRS)
all: $(DIRS)
# ... do stuff in this direcotry ...
$(DIRS):
$(MAKE) -C $(@)
clean:
# ... Clean this directory ...
$(foreach d,$(DIRS),cd $(d) && $(MAKE) clean; )
由于我已经将目录名称用作构建目标,因此我想使用循环来清理每个子目录。但我收到以下错误:
$ make clean
# ... Clean this directory ...
cd dir1 && make clean; cd dir2 && make clean;
make[1]: Entering directory `/home/ex/clean_try/dir1'
rm -fr *.o
make[1]: Leaving directory `/home/ex/clean_try/dir1'
/bin/sh: 1: cd: can't cd to dir2
make: *** [clean] Error 2
好像已经离开dir1了,怎么从主目录进不了dir2?
【问题讨论】: