gpt4 book ai didi

makefile - 为什么 GNU make 删除文件

转载 作者:行者123 更新时间:2023-12-02 17:04:49 24 4
gpt4 key购买 nike

我有一个稍微有点 hackish 的 makefile 用于运行测试:

### Run the tests

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

test: $(tests)

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

它确实有效,但它让 Make 做了一些我以前从未见过它做的事情。我的测试当前已损坏,并导致总线错误。 Make 给出以下输出:

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

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

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

最佳答案

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

<小时/>

您的情况是否需要这种行为取决于测试的性质。如果您计划修复测试以使其不会导致总线错误,那么删除目标并不是什么大问题。如果您想稍后使用目标进行调试,则需要更改 make 过程。

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

<小时/>

另一个可能是:

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

未经测试,但 documentation表示目标不会被删除:

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.

和:

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.

关于makefile - 为什么 GNU make 删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/588550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com