Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recompile when modified c++ header?

I have a *.H c++ headers in include/c++ folder, but even if I modified these files, make doesn't make again, what can I modify my Makefile to remake when those files modified?

like image 820
mikezang Avatar asked Oct 15 '25 16:10

mikezang


2 Answers

If you're using GNU make and GCC, and building separate objects for each source (rather than building a program directly from all the sources) you can generate the necessary dependencies automatically. Add the following the the compiler's command-line arguments:

-MD -MP

-MD will generate a .d file alongside each object, containing make rules specifying all the headers it depends on. You could instead use -MMD to exclude system headers (ones included with <> rather than ""), if you don't expect these to change. -MP will generate dummy rules to ensure the target is rebuilt if any of the headers are deleted.

Then include all the .d files from the makefile, using -include so it's not an error if they're missing. I do this by transforming the list of object files:

-include $(all_objs:.o=.d)
like image 180
Mike Seymour Avatar answered Oct 17 '25 05:10

Mike Seymour


Just make sure that your target depends on your headers.

your_app: $(SOURCES) $(HEADERS)
  $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(SOURCES) $(LIBS) -o$@
like image 42
dnk Avatar answered Oct 17 '25 05:10

dnk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!