4

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?

Riking
  • 2,389
  • 1
  • 24
  • 36

1 Answers1

4

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.

Riking
  • 2,389
  • 1
  • 24
  • 36
  • And you can have `make` do not delete intermediate files by creating a `.NOTINTERMEDIATE` target with no prerequisites, as I understand it. – Jakub Narębski Nov 19 '22 at 20:25