我有一个包含多个子目录的项目,主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?
答案 0 :(得分:2)
cd dir2
是刚刚完成cd dir1
---的背景,而dir2不是dir1的子目录,对吧? : - )
尝试:make -C $d clean
HTH