why make clean triggers other rules

"lijh.8" via Users list for the GNU implementation of make <[email protected]> Tue, 6 May 2025 20:16:01 +0800
Newsgroups gmane.comp.gnu.make.general
Message-ID <[email protected]>
Hi community,


1.&nbsp;


Why will the rule for `.d` dependency file be triggered when I do `make clean`?
Why it tries to stop me from deleting the temporary `.d` file?


I define an explicit pattern rule for `.o` object file,&nbsp;
and there is also an implicit rule for it.
Why rule for `.o` object file is not triggered?


2.


And that sed command put the `.d` file to the target,&nbsp;
so if a `.d` file is deleted, it will be regenerated.
If i do not use this the rule `.d` dependency and just put ` CPPFLAGS := -MMD `,&nbsp;
when a `.d` file is deleted, it will not be generated.


Am I correct?


Thanks


```
CFLAGS&nbsp; &nbsp;:= -g&nbsp;
CPPFLAGS :=&nbsp;


main: $(patsubst %.c,%.o,$(wildcard *.c))&nbsp;
-include $(patsubst %.c,%.d,$(wildcard *.c))&nbsp;
clean: ; $(RM) *.o *.d main
.PHONY: clean


%.o: %.c&nbsp;
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<&nbsp;&nbsp;


%.d: %.c&nbsp;
	set -e; rm -f $@; \
	$(CC) -MM -MP $(CPPFLAGS) $< &gt; $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ &gt; $@; \
	rm -f $@.$$$$


```


---


```
$ ls
foo.h&nbsp; main.c&nbsp; Makefile
$&nbsp;
$ make
set -e; rm -f main.d; \
cc -MM -MP -g&nbsp; &nbsp;main.c &gt; main.d.$$; \
sed 's,\(main\)\.o[ :]*,\1.o main.d : ,g' < main.d.$$ &gt; main.d; \
rm -f main.d.$$
cc -g&nbsp; &nbsp;-c -o main.o main.c&nbsp;&nbsp;
cc&nbsp; &nbsp;main.o&nbsp; &nbsp;-o main
$&nbsp;
$ ls
foo.h&nbsp; main&nbsp; main.c&nbsp; main.d&nbsp; main.o&nbsp; Makefile
$&nbsp;
$ make clean
rm -f *.o *.d main
$&nbsp;
$ ls
foo.h&nbsp; main.c&nbsp; Makefile
$&nbsp;
$ make clean
set -e; rm -f main.d; \
cc -MM -MP -g&nbsp; &nbsp;main.c &gt; main.d.$$; \
sed 's,\(main\)\.o[ :]*,\1.o main.d : ,g' < main.d.$$ &gt; main.d; \
rm -f main.d.$$
rm -f *.o *.d main
$&nbsp;
$ ls
foo.h&nbsp; main.c&nbsp; Makefile
$&nbsp;
```