c - GNU Make 无法从循环中进入子目录

标签 c linux makefile gnu-make

我有一个包含多个子目录的项目,主 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/

相关文章:

c - 实现特征结构 : what data type to use?

c - 如何从 C 编程中的函数返回结构变量?

php - Wordpress 函数在 bash 中运行的 php 脚本中不起作用

c - 如何在 posix_openpt() 中定义名称

linux - 帮助调试具有正确脚本路径并在手动触发时工作的 cron 作业

linux - 运行并行 make 时出错

c - 带有 TCP 流的 TLS/SSL 中的 ssl_read() 不返回由 BIO_write() 写入的整个缓冲区

cuda单独编译 "undefined methods"

makefile - 使 : Get dependencies of a target inside another one

c - 它是头文件还是库?在生成文件中