简体   繁体   English

如何避免“没有这样的文件或目录”错误的`make clean` Makefile目标

[英]How to avoid “No such file or directory” Error for `make clean` Makefile target

I have a Makefile that defines a .PHONY clean target for cleaning up .o files and executables, that target looks like: 我有一个Makefile,它定义了.PHONY清理目标,用于清理.o文件和可执行文件,目标如下:

...
.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)

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会引发错误(没有这样的文件或目录),并且输出显示Makefile目标失败,当它明显做我想要的时候。

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"? 是否有一种很好的方法可以告诉rm命令“删除这些文件,如果它们存在,如果不存在就不要抱怨”? I looked up the manpage for rm , and found no such flag. 我查了手册页的rm ,发现没有这样的标志。

Edit: I actually didn't notice the description of the -f flag in the manpage, this is the solution. 编辑:我实际上没有注意到联机帮助页中-f标志的描述,这是解决方案。

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

rm -f rm -f

will FORCE and not output any error 将FORCE并且不输出任何错误

When Targets Fail 当目标失败时

When a target is executed, it returns a status based on whether or not it was successful--if a target fails, then make will not execute any targets that depend on it. 执行目标时,它会根据事件是否成功返回状态 - 如果目标失败,则make将不会执行任何依赖它的目标。 For instance, in the above example, if "clean" fails, then rebuild will not execute the "build" target. 例如,在上面的示例中,如果“clean”失败,则rebuild将不会执行“build”目标。 Unfortunately, this might happen if there is no core file to remove. 不幸的是,如果没有要删除的核心文件,可能会发生这种情况。 Fortunately, this problem can be solved easily enough by including a minus sign in front of the command whose status should be ignored: 幸运的是,通过在命令前面加一个减号,可以很容易地解决这个问题,其状态应该被忽略:

 clean: -rm -f *.o core 

~ http://www.cprogramming.com/tutorial/makefiles.html ~ http://www.cprogramming.com/tutorial/makefiles.html

I've given up with rm. 我放弃了rm。 The following command will remove files and dirs. 以下命令将删除文件和目录。

find . -delete

To remove only files or only dirs, there is the -type option: 要仅删除文件或仅删除目录,请使用-type选项:

# remove only files
find . -type f -delete


# remove only dirs
find . -type d -delete

Actually, I've create a little script (based on that snippet) named bomb that removes files without complaining: https://github.com/lingtalfi/bomb 实际上,我已经创建了一个名为bomb的小脚本(基于该片段),可以在不抱怨的情况下删除文件: https//github.com/lingtalfi/bomb

Late to the party, but here's another solution that worked with our quirky build environment: 晚会,但这是另一个解决方案,适用于我们古怪的构建环境:

if exist *.exe rm -f *.exe

Not output free, but reduced and exits cleanly: 不是免费输出,但是减少并且干净地退出:

# make clean
        if exist *.exe rm -f *.exe

I tried a lot of alternatives that all had issues before settling on this one. 在解决这个问题之前,我尝试了许多替代方案,但都有问题。

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

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