为什么我没有指定 make add rm 命令?

Why does make add rm commands I didn't specify?

我刚刚创建了一个通用的 makefile 来编译和 link 汇编文件:

AS=nasm
ASFLAGS=-g -f elf64
LDFLAGS=-m elf_x86_64 -static
BINARIES=print_args64

all: $(BINARIES)

%: %.o
        $(LD) $(LDFLAGS) -o $@ $<

%.o: %.asm
        $(AS) $(ASFLAGS) -o $@ $<

clean:
        $(RM) $(BINARIES) $(wildcard *.o)

运行 make all 我期望以下内容:

nasm -g -f elf64 -o print_args64.o print_args64.asm
ld -m elf_x86_64 -static -o print_args64 print_args64.o

但它实际上为以下对象添加了一个rm命令:

 rm print_args64.o

这是从哪里来的,我该如何避免?

我找不到这方面的任何文档。

问候,bvolkmer

make 删除它是因为它是一个中间文件,而不是因为您可能认为的 clean 目标。参见 chapter 10.4 Chains of Implicit Rules in the manual。相关语录:

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.

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.

You can list the target pattern of an implicit rule (such as ‘%.o’) as a prerequisite of the special target .PRECIOUS to preserve intermediate files made by implicit rules whose target patterns match that file’s name

另请注意,您可以使用 -d 开关向 make 询问有关它正在做什么的额外信息,它会打印如下内容:

Successfully remade target file `all'.
Removing intermediate files...
rm print_args64.o