为什么这个makefile删除了我的目标?

问题描述:

一个最小的例子:

%.txt: foo.log
    # pass

%.log:
    # pass

运行:

$ make a.txt --dry-run
# pass
# pass
rm foo.log

为什么最后一个动作是rm foo.log? 如何摆脱它?

Why the last action is rm foo.log? How to get rid of it?

默认情况下,GNU make会删除中间文件.由于%.txt取决于%.log,因此make希望删除.log文件.为了防止这种行为,您可以使用.PRECIOUS或.SECONDARY将它们标记为珍贵.

By default, GNU make removes intermediate files. Since %.txt depends on %.log, make wants to remove the .log file. To prevent that behavior you mark them as precious with .PRECIOUS or .SECONDARY.

.PRECIOUS: foo.log

此外,您可以使用.SECONDARY而不依赖任何文件,从而不会删除任何中间文件.

Also, you can make it so that no intermediate files are ever removed by using .SECONDARY with no dependencies.

.SECONDARY:

请参见GNU的部分制作手册.

See this section of the GNU make manual.