使用Makefile清理子目录 [英] Using Makefile to clean subdirectories

查看:895
本文介绍了使用Makefile清理子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to perform a make clean from the parent directory which also recursively cleans all sub-directories without having to include a makefile in each sub-directory?

例如,当前在我的Makefile中,我有类似的内容:

For example, currently in my Makefile, I have something like:

SUBDIRS = src, src1

.PHONY: clean subdirs $(SUBDIRS)

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

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

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

However, this requires me to have a Makefile in both src and src1. Otherwise, I would get the error

No rule to make target clean

由于我只想在每个子目录中运行命令"rm -rf * .o 〜core .depend. .cmd * .ko * .mod.c",因此似乎多余必须在每个子目录中包含一个Makefile,并使用完全相同的行进行清理.没有办法简单地在每个子目录中运行相同的clean命令吗?

Since I only want to run the command "rm -rf *.o ~ core .depend ..cmd *.ko *.mod.c" in each subdirectory anyways, it seems redundant to have to include a Makefile in every subdirectory with the exact same line for clean. Is there no way to simply have the same clean command run in each of the subdirectories?

推荐答案

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

I agree that you could just have the rm command operate on subdirs. But something like the following allows recursive make using only a single makefile:

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

这篇关于使用Makefile清理子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆