I just created a general purpose makefile to compile and link assembler files:
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)
Running make all
I would expect following:
nasm -g -f elf64 -o print_args64.o print_args64.asm
ld -m elf_x86_64 -static -o print_args64 print_args64.o
But it actually adds a rm
command for the object in the following:
rm print_args64.o
Where does this come from and how can I avoid it?
I can't find any documentation for this.
Greetings, bvolkmer
make
deleted it because it was an intermediate file not because of the clean
target you might think. See chapter 10.4 Chains of Implicit Rules in the manual. Relevant quotes:
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.
and
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
Also note that you can ask make
for extra information about what it is doing using the -d
switch, which prints something like:
Successfully remade target file `all'.
Removing intermediate files...
rm print_args64.o