If you are using a GNU compiler, the compiler can assemble a list of dependencies for you. Makefile fragment:
depend: .depend
.depend: $(SRCS)
rm -f "$@"
$(CC) $(CFLAGS) -MM $^ -MF "$@"
include .depend
or
depend: .depend
.depend: $(SRCS)
rm -f "$@"
$(CC) $(CFLAGS) -MM $^ > "$@"
include .depend
where SRCS
is a variable pointing to your entire list of source files.
There is also the tool makedepend
, but I never liked it as much as gcc -MM
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…