如何避免“没有这样的文件或目录"? Make make Makefile目标错误

如何避免“没有这样的文件或目录

问题描述:

我有一个Makefile,它定义了一个.PHONY干净目标来清理.o文件和可执行文件,该目标看起来像:

I have a Makefile that defines a .PHONY clean target for cleaning up .o files and executables, that target looks like:

...
.PHONY : clean
clean:
    rm $(addprefix $(vq_DIR),$(vq_OBJS)) \
       $(addprefix $(vq_DIR),vq) \
       $(addprefix $(covq_DIR),$(covq_OBJS)) \
       $(addprefix $(covq_DIR),covq) \
       $(addprefix $(covq_2_DIR),$(covq_2_OBJS)) \
       $(addprefix $(covq_2_DIR),covq_2) \
       $(addprefix $(covq_2_DIR),$(test_OBJS)) \
       $(addprefix $(covq_2_DIR),test)

一切正常,但是当其中一些文件不存在时,rm引发错误(没有此类文件或目录),并且输出显示Makefile目标失败,这显然是我想要的

Everything works as it should, but when some of these files do not exist, rm raises an Error (No such file or directory), and the output says that the Makefile target failed, when it clearly did what I wanted.

是否有一种很好的方法可以基本告诉rm命令删除这些文件(如果存在),并且不要抱怨,如果它们不存在"?我在rm的联机帮助页中查找,但没有找到这样的标志.

Is there a good way to basically tell the rm command to "remove these files if they exist, and don't complain if they don't"? I looked up the manpage for rm, and found no such flag.

实际上,我没有注意到联机帮助页中-f标志的描述,这是解决方案.

I actually didn't notice the description of the -f flag in the manpage, this is the solution.

使用rm -f(甚至更好的$(RM),由内置make规则提供,可以使用make -p找到) clean规则中的rm.

Use rm -f (or even better $(RM), provided by built-in make rules, which can be found out using make -p) instead of rm in your cleanrule.