darksky
darksky

Reputation: 21019

Makefile not deleting intermediate files

My Makefile is not deleting intermediate files. I've set .INTERMEDIATE. Here is my Makefile:

OBJECTS=prefixer.o stack.o
CFLAGS=-Werror -Wmissing-prototypes -g
LIBS=-lm
CC=gcc

prefixer : $(OBJECTS)
    $(CC) -o prefixer $(OBJECTS) $(LIBS)

prefixer.o : stack.h
    $(CC) -c prefixer.c -o $@ $(CFLAGS)
stack.o : stack.c stack.h
    $(CC) -c stack.c -o $@ $(CFLAGS)

.INTERMEDIATE: %.o

.PHONY: clean
clean : 
    -rm prefixer *.o

What is wrong with .INTERMEDIATE?

Upvotes: 3

Views: 3201

Answers (3)

See Gnu make special targets for role of .INTERMEDIATE

You probably mean

 .SECONDARY:  $(OBJECTS)

because I think that if you put .INTERMEDIATE then the objects will always be removed, and you probably don't want that. I don't think you can meaningfully put %.o as prerequisites of special targets.

I would suggest to keep the object files so you don't waste time recompiling them.

Running once make -p will teach you a lot about the rules that make knows about.

Using remake -x (i.e. remake) is very helpful, at least to debug complex Makefile-s. You could also use make -d but I find it too verbose.

You may want to use better builders than make e.g. omake.

Upvotes: 8

Beta
Beta

Reputation: 99094

The special target .INTERMEDIATE doesn't work with the % wildcard. Try this:

.INTERMEDIATE: stack.o prefixer.o

Upvotes: 4

steffen
steffen

Reputation: 8968

As far as I understand, .INTERMEDIATE is not supposed to delete the intermediate files if it already exists. This is from the documentation of gnu make:

Intermediate files are remade using their rules just like all other files. But intermediate files are treated differently in two ways.

The first difference is what happens if the intermediate file does not exist. If an ordinary file b does not exist, and make considers a target that depends on b, it invariably creates b and then updates the target from b. But if b is an intermediate file, then make can leave well enough alone. It won't bother updating b, or the ultimate target, unless some prerequisite of b is newer than that target or there is some other reason to update that target.

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.

So make sure there are no *.o in the directory before running make.

Upvotes: 0

Related Questions