0

My Makefile is below, i used the .d files for auto-dependencies,but it does not work when i just modified some .h files, it's strange, but why.. thanks for you help

PROGRAM   := a.out 
SRCDIRS   := ./src/access
INCLUDE   := -I./include/access
SRCEXTS   := .cpp
CPPFLAGS  := -g -Wall
LDFLAGS   := 

CXX     = g++
RM     = rm -f

SHELL   = /bin/sh
SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
OBJS    = $(foreach x,$(SRCEXTS), \
      $(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES))))
DEPS    = $(patsubst %.o,%.d,$(OBJS))
.PHONY : all objs clean cleanall rebuild
all : $(PROGRAM)


objs : $(OBJS)
%.o : %.cpp
    $(CXX) -c $(CPPFLAGS) $< -o $@ $(INCLUDE)

$(PROGRAM) : $(OBJS)
    $(CXX) -o $(PROGRAM) $(OBJS) $(LDFLAGS) 
rebuild: clean all
clean :
    @$(RM) $(OBJS) $(DEPS)
cleanall: clean
    @$(RM) $(PROGRAM) 

-include $(DEPS)
%.d : %.cpp
    rm -f $@; $(CXX) -MM $< $(INCLUDE) > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$
  • Stop using ancient dependency generation code, GCC has had flags like [`MMD` and `MP`](http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/) for over 15 years – user657267 Jul 27 '16 at 07:32

1 Answers1

0

There is nothing which causes the %.d pattern to run because %.o does not depend on it. So per your makefile, Make simply uses whichever $(DEPS) files already existed on the disk.

With the dependencies you have declared, the %.d file would only be regenerated if something which depended on it required it, and the corresponding %.cpp file had been modified.

Adding %.d to the %.o dependencies should add the required dependency, though you might still need to ensure somehow that %.d runs before you -include.

Ideally, each %.d file should depend on whatever %.h files the corresponding %.cpp file depends on, but the customary solution to the chicken-and-egg problem is to force rebuilding the %.d files more often than strictly necessary.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • For more on the topic, see e.g. http://stackoverflow.com/questions/297514/how-can-i-have-a-makefile-automatically-rebuild-source-files-that-include-a-modi – tripleee Jul 27 '16 at 07:22