在 makefile 中保留由通配符定义的中间文件

keep intermediate files defined by wildcards in makefile

我用 Makefile 定义了一系列数据处理步骤,但发现属于中间步骤的文件被 Make 删除了。在以下示例中,文件 processed_%.txt 始终被删除。

#make some simple data
#echo "test data X" > test_x.txt
#echo "test data y" > test_y.txt

x = test_x.txt
y = test_y.txt

#these are deleted
processed_%.txt: ${x} ${y}
        cat $< > $@

#these remain in the directory
processed_again_%.txt: processed_%.txt
        cat $< > $@

all: processed_again_x.txt processed_again_y.txt

谁能解释发生了什么以及如何disable/control这种行为?

谢谢, zachcp

这就是 chains of implicit rules 的工作方式。

The second difference is that if make does create b in order to update something else, it deletes b later on after it is no longer needed. Therefore, an intermediate file which did not exist before make also does not exist after make. make reports the deletion to you by printing a ‘rm -f’ command showing which file it is deleting.

您可以通过将文件标记为 .SECONDARY

来控制此行为

You can prevent automatic deletion of an intermediate file by marking it as a secondary file. To do this, list it as a prerequisite of the special target .SECONDARY. When a file is secondary, make will not create the file merely because it does not already exist, but make does not automatically delete the file. Marking a file as secondary also marks it as intermediate.