使用Makefile清理子目录

Ton*_*ony 5 makefile gnu-make

是否可以从父目录执行make clean,它还递归清理所有子目录,而不必在每个子目录中包含makefile?

例如,目前在我的Makefile中,我有类似的东西:

SUBDIRS = src, src1

.PHONY: clean subdirs $(SUBDIRS)

clean: $(SUBDIRS)
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c

$(SUBDIRS):
    $(MAKE) -C $(SUBDIRS) clean 
Run Code Online (Sandbox Code Playgroud)

但是,这要求我在src和src1中都有一个Makefile.否则,我会得到错误

No rule to make target clean
Run Code Online (Sandbox Code Playgroud)

因为我只想在每个子目录中运行命令"rm -rf*.o ~core.depend .cmd*.ko*.mod.c",所以在每个子目录中都包含一个Makefile似乎是多余的.完全相同的线清洁.有没有办法简单地在每个子目录中运行相同的清洁命令?

小智 6

我同意你可以让 rm 命令对子目录进行操作。但是类似下面的内容允许仅使用单个 makefile 进行递归 make:

SUBDIRS = . src src1
SUBDIRSCLEAN=$(addsuffix clean,$(SUBDIRS))

clean: $(SUBDIRSCLEAN)

clean_curdir:
    rm -rfv *.o *~ core .depend .*.cmd *.ko *.mod.c

%clean: %
    $(MAKE) -C $< -f $(PWD)/Makefile clean_curdir
Run Code Online (Sandbox Code Playgroud)