简体   繁体   English

如何在Makefile配方中实现for循环

[英]How is a for loop implemented in a Makefile recipe

Although the below script, is intended find a makefile in the sub directory level of 1 and make them recursively, I'm not able to find any success with it. 尽管下面的脚本旨在在子目录级别1中查找一个makefile并使其递归进行,但我无法通过它找到任何成功。

for file in `find $(pwd)/ -maxdepth 2 -mindepth 2 -type f -name 'Makefile'`; do\
    make --directory=$(dirname $file);\
done

What might be the problem here? 这可能是什么问题?

In this recipe you do not intend $(pwd) , $(dirname ...) and $file to be expanded by make , but they are are, because you have not escaped $ . 在此配方中,您不打算通过make扩展$(pwd)$(dirname ...)$file ,但是它们是这样的,因为您没有逃脱$ (Strictly, you do not intend the $f in $file to be expanded by make ). (严格来说,您不希望通过make扩展$file$f )。 Re-write as: 重新写成:

for file in `find $$(pwd)/ -maxdepth 2 -mindepth 2 -type f -name 'Makefile'`; do\
    make --directory=$$(dirname $$file);\
done

Later 后来

Is there a better and simple way of making sub modules of a project? 有没有更好,更简单的方法来制作项目的子模块?

Yes. 是。 The canonical way would be like: 规范的方式将是:

# Identify the sub-directories some way, not necessarily like this:
SUBDIRS := $(dir $(shell find $$(pwd)/ -maxdepth 2 -mindepth 2 -type f -name 'Makefile'))

.PHONY: $(SUBDIRS)

all: $(SUBDIRS)
# Maybe some recipe...

$(SUBDIRS):
    $(MAKE) -C $@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM