Hi community,

1. 


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, 
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, 
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 
`, 
when a `.d` file is deleted, it will not be generated.


Am I correct?


Thanks


```
CFLAGS   := -g 
CPPFLAGS := 


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


%.o: %.c 
        $(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;
```

Reply via email to