【问题标题】:GNU Make fails to enter subdirectories from a loopGNU Make 无法从循环中进入子目录
【发布时间】: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?

【问题讨论】:

    标签: c linux makefile gnu-make


    【解决方案1】:

    cd dir2 是在刚刚完成 cd dir1 的上下文中 --- 并且 dir2 不是 dir1 的子目录,对吧? :-)

    试试:make -C $d clean

    HTH

    【讨论】:

    • 好的,成功了。但是......它怎么会在错误之前说:Leaving directory '/home/ex/clean_try/dir1'?这令人费解,至少可以说......
    • 这是正常的 make-within-make 行为。可以用--no-print-directory抑制
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多