我有一个包含多个子目录的项目,主 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呢?
最佳答案
cd dir2
是在刚刚完成 cd dir1
的上下文中 --- 而 dir2 不是 dir1 的子目录,对吗? :-)
尝试:make -C $d clean
HTH
关于c - GNU Make 无法从循环中进入子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855986/