makefile – 为什么GNU make delete一个文件

前端之家收集整理的这篇文章主要介绍了makefile – 为什么GNU make delete一个文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个稍微hackish makefile运行测试:
### Run the tests

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

test: $(tests)

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

它的作品,但它使得做一些我以前从未见过的事情.我的测试目前被打破,导致总线错误. 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进行更改.

删除目标的一种方法是使用.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.

原文链接:https://www.f2er.com/c/113448.html

猜你在找的C&C++相关文章