Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GNU make adding this 'rm' command at the end? [duplicate]

I created a Makefile, but when I use it, make seems to be adding rm commands at the end for some reason.

Here's the Makefile, stripped of only the full contents of FILENAMES and TESTS: https://gist.github.com/riking/9a1dff3f1c1b36e6dbfce53e52a325ff

Edit: Here's the rules that ended up mattering.

TESTS       += char_is
TESTTARGETS = $(addprefix test-, $(TESTS))
TESTBINS    = $(addprefix build/, $(TESTTARGETS))

build/%.o: %.c libft.h | build
    $(CC) $(CFLAGS) -c $< -o $@

test: $(TESTBINS)
    for bin in $(TESTBINS); do \
        echo $$bin ; \
        $$bin ; \
        echo ; \
    done

build/test-%: build/test_%.o libft.a | build
    $(CC) $(LDFLAGS) -o $@ $^

When I run make re test, the output ends with this:

.....
build/test-memmove

rm build/test_ft_memcpy.o ... build/test_char_is.o

(one object file for every element of $(TESTS))

Why the heck is it deleting the object files?

like image 964
Riking Avatar asked Sep 14 '25 07:09

Riking


1 Answers

The object files for the test binaries are intermediate products, because the test binaries are created using implicit rules, as opposed to the libft.a archive, which is created with an explicit rule.

Because they're intermediate products of a chain of pattern rules, they're deleted at the end of the build.

The Make manual page that talks about this is Chains of Implicit Rules.

like image 184
Riking Avatar answered Sep 17 '25 20:09

Riking