Create file | Dependence on another header file included in the header file

Suppose I have a rule below at Makefile.

test.o: test.cpp foo.h
        g++ -c -o test.o test.cpp

Now suppose that foo.hincludes bar.h, as shown below.

user $ head -n 5 foo.h
#include"bar.h"
/*
.
.
*/
user $  

Will it be built test.oagain if there is any change in bar.h?

Or should I specifically specify bar.hin the rule, as shown below:

test.o: test.cpp foo.h bar.h
        g++ -c -o test.o test.cpp
+4
source share
2 answers

Will it be built test.oagain if there is any change in bar.h?

No. Make is unable to find out about this dependency or check for changes in #includes.

, , , : . (, GCC GNU .)

  • .

  • .

    SRCFILES := ...
    
  • , .d SRCFILE.

    DEPFILES := $(patsubst %.cpp,%.d,$(SRCFILES))
    
  • Makefile. ( - , Make , , , .)

    -include $(DEPFILES)
    
  • , .

    %.o: %.cpp Makefile
        @$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@
    

    -MMD make, () , , *.d. -MP , , .

+4

GCC (, , Clang) ; , (cpp):

depend: .depend

.depend: $(SRC_FILES)
        rm -f ./.depend
        $(CC) $(CFLAGS) -MM $^ -MF  ./.depend;

include .depend

%.o: %.cpp
    $(CC) $(CFLAGS) -c $<

makedepend.

+2

Source: https://habr.com/ru/post/1619617/


All Articles