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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...