Hacker News new | ask | show | jobs
by DblPlusUngood 2690 days ago
A particularly tricky task with GNU make is automatically adding target dependencies on header files and handling updates to them (gcc's -M and -MMD switches). It would be great if the article explained those best practices, too.
3 comments

Thanks John, your book is the most-referred to on my office shelf ;)

Edit: https://nostarch.com/gnumake (via https://blog.jgc.org/2015/04/the-gnu-make-book-probably-more...)

+1 for a great book, I bought that book too!

I use the patterns in @jgrahamc's article "Escaping: A Walk on the Wild Side" pretty often. Especially the \n definition, which works great to put on the end of a $(foreach) loop inside of a recipe.

@jgrahamc: Thanks for all you've written on Make over the years.

I also recommend this book. The series on “meta programming make” is also quite good: http://make.mad-scientist.net/category/metaprogramming/
Ooh. That's great. I had not seen that!
That'a great. Glad it was helpful. If you have any of your own GNU Make tips and tricks please do blog about them!
Here's all you need to track header file dependencies. Basically, when compiling a .cpp file to generate a .o file, a corresponding .deps file is also generated. And at make startup, we include all the .deps files we find in the bin (output) directory, if there's one.

$(BIN)/%.cpp.o: %.cpp

     $(CXX)  -c $(CXXFLAGS) "$<" -o "$@"

     @$(CXX) -c $(CXXFLAGS) "$<" -o "$@.deps" -MP -MM -MT "$@"
-include $(shell test -d $(BIN) && find $(BIN) -name "*.deps")
This tiny Makefile should convey the idea.

    CFLAGS=-MD -MP

    prog: <list of .o files>
        $(CC) -o $@ $^
        
    -include *.d