PDA

View Full Version : Help getting dependencies working with Makefile


maxxjr
December 15th, 2009, 12:45 PM
I am trying to write a Makefile so that if a header file changes, anything that uses that header file will be rebuilt. I have found a lot of info on the web, but it's not quite working for me.

First question--are there other forums that might be better for this type of question?

If I 'touch' the header file, or if I go in and just add a random character to a comment, no rebuild is done.

Here is my Makefile:

MODULES := AR

LIBS :=

SRCS :=

include Makefile.in

include $(patsubst %, %/module.mk, $(MODULES))

.SUFFIXES: .o .so

%.o: %.cpp
$(CPP) -MMD -c $(INCLUDES) $(CFLAGS) $*.cpp -o $*.o
@cp -f $*.d $*.d.tmp
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' | sort | uniq >> $*.d
@rm -f $*.d.tmp


OBJ := $(patsubst %.cpp,%.o, $(filter %.cpp,$(SRCS)))

depend:

prog: $(OBJ)
$(CPP) -o $@ $(OBJ) $(LIBS)

clean:
rm $(patsubst %.cpp,%.o, $(filter %.cpp,$(SRCS)))

cleanall:
-rm $(patsubst %.cpp,%.o, $(filter %.cpp,$(SRCS)))
-rm $(patsubst %.cpp,%.d, $(filter %.cpp,$(SRCS)))
-rm $(patsubst %.cpp,%.P, $(filter %.cpp,$(SRCS)))

all: prog

-include $(OBJ:.o=.d)

The dependency files generated by the %.o: %.cpp rule looks correct. The rule is based on one of the websites I found in my search (http://make.paulandlesley.org/autodep.html).


The last include is for these dependency files, which contains something like:

object.o: object.cpp header1.h header2.h
object.cpp:
header1.h:
header2.h:

From my research, if header1.h changes, anything that depends on header1.h will be rebuilt. So object.o should be rebuilt. This doesn't happen.

Thanks for any help!

Some Penguin
December 17th, 2009, 06:46 PM
Header files are just header files, not build targets. You don't need a rule for a header file as a target.

You might also want to look into 'makedepend'.