Makefile removes object files for no reason

The files are being removed because make considers them “intermediate”. When make forms a chain of rules to produce a prerequisite, it treats all files created by the intermediate chains as “intermediate” and removes then when the target is created. See Chained Rules in the manual for GNU make.

In your case, you can prevent this in two ways: Get rid of the %.bin rule, which seems wrong anyway, because it says that all .bin files depend on a fixed list of object files, and replace it by

$(BIN): $(OBJS)
        # as before

or mark the object files as “secondary”:

.SECONDARY: $(OBJS)

Leave a Comment