GNU为什么要删除文件 [英] Why does GNU make delete a file

查看:82
本文介绍了GNU为什么要删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行测试的文件有点黑:

I've got a slightly hackish makefile for running tests:

### Run the tests

tests := tests/test1 tests/test2 ...

test: $(tests)

$(tests): %: %.c
    gcc -o $@ $(testflags) $<
    $@

它可以工作,但是可以使Make做一些我从未见过的事情.我的测试当前已损坏,并导致总线错误. Make提供以下输出:

It works, but it makes Make do something I've never seen it do before. My test is currently broken, and causes a bus error. Make gives the following output:

gcc -o tests/test1 [flags blah blah] tests/test1.c
tests/test1
make: *** [tests/test1] Bus error
make: *** Deleting file `tests/test1'

我对最后一行很好奇.我之前从未见过Make做到这一点.为什么Make会删除已编译的测试?

I'm curious about the last line. I've never seen Make do that before. Why does Make delete the compiled test?

注意:我对本示例进行了大量编辑,以使其更简单.我可能会引入一些错误.

Note: I edited this example pretty heavily to make it simpler. I might have introduced some mistakes.

推荐答案

因为目标可能没有正确构建.下次您make项目时,它将尝试重建目标.如果未删除文件,则make将无法知道出现了问题. make无法知道失败是来自测试,而不是构建目标的过程.

Because the target might not have been built correctly. The next time you make the project, it will attempt to rebuild the target. If the file had not been removed, make would have no way of knowing something went wrong. make can't know that the failure was coming from a test rather than the process that builds the target.

在您的情况下是否需要此行为取决于测试的性质.如果计划修复测试以免导致Bus error,则删除目标并不重要.如果您想稍后使用目标进行调试,则需要对制作过程进行更改.

Whether or not this behavior is desirable in your case depends on the nature of the tests. If you plan on fixing the test so that it does not cause a Bus error, removing the target isn't a big deal. If you want to use the target for debugging later, you'll need to make a change to your make process.

不删除目标的一种方法是使用.PRECIOUS目标.

One way to not delete targets is to use the .PRECIOUS target.

另一个可能是:

$(tests): %: %.c
    gcc -o $@ $(testflags) $<
    -$@

未经测试,但文档指示目标将无法删除:

Not tested, but the documentation indicates the target will not be removed:

当发生无法告知make的错误时,这意味着不能正确地重制当前目标,并且任何直接或间接依赖它的目标也不能正确地进行重造.由于尚未达到目标的前提条件,因此将不再对这些目标执行任何命令.

When an error happens that make has not been told to ignore, it implies that the current target cannot be correctly remade, and neither can any other that depends on it either directly or indirectly. No further commands will be executed for these targets, since their preconditions have not been achieved.

和:

通常,当命令失败时,如果它完全更改了目标文件,则该文件已损坏且无法使用-至少它没有完全更新.但是该文件的时间戳表明它现在是最新的,因此下次运行make时,它将不会尝试更新该文件.这种情况与命令被信号杀死的情况相同.请参阅中断.因此,通常的正确做法是,如果在开始更改文件后命令失败,则删除目标文件.如果.DELETE_ON_ERROR出现为目标,make将执行此操作.这几乎总是您想做的,但这不是历史做法.因此,出于兼容性考虑,您必须明确要求它.

Usually when a command fails, if it has changed the target file at all, the file is corrupted and cannot be used—or at least it is not completely updated. Yet the file's time stamp says that it is now up to date, so the next time make runs, it will not try to update that file. The situation is just the same as when the command is killed by a signal; see Interrupts. So generally the right thing to do is to delete the target file if the command fails after beginning to change the file. make will do this if .DELETE_ON_ERROR appears as a target. This is almost always what you want make to do, but it is not historical practice; so for compatibility, you must explicitly request it.

这篇关于GNU为什么要删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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