微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/113448.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐