2

I have the following rule:

SPECIAL = file1 file2

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

I would like that if % is in $(SPECIAL), then b.h is added to the list of dependencies.

Is there a way to do it, without repeating the rule?

Fabio
  • 2,105
  • 16
  • 26
  • 2
    The answer is good, but a better way is using automatic dependency generation (google for it) for all dependencies on header files –  Jun 07 '17 at 14:57
  • That error message looks like it comes from gcc, not make. Is there code in your compile unit TypesForArticle.h or why are you compiling a header file? – Vroomfondel Jun 08 '17 at 15:09
  • You assigned the recipe to the wrong rule in Case 2. Swap the last line with the previous one. – Maxim Egorushkin Jun 08 '17 at 15:17
  • That works. Thank you. You may consider editing your answer, clearly showing in which position of the code your extra line goes. As a newbie, I misinterpreted the meaning of 'after the rule'. – Fabio Jun 08 '17 at 15:38
  • @Fabio Updated my answer. I assumed that the recipe is a part of a rule. – Maxim Egorushkin Jun 08 '17 at 17:08

1 Answers1

4

You can assign additional dependencies separately. Just add a line at the end:

$(addsuffix .o,${SPECIAL}): b.h

To not have to deal with dependency order, replace $< in the rule with $(filter %.cpp,$^). This way %.cpp does not have to be the first dependency.


Ideally, you want the header dependencies to be generated automatically to avoid specifying them manually.

The most simple automatic dependency generation:

%.o : %.cpp 
    $(CXX) -c -o $@ -MD -MP $(CXXFLAGS) $(filter %.cpp,$^)

ifneq ($(MAKECMDGOALS),clean)
-include $(wildcard *.d)
endif   
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Can't get it too work (in the first version). Could you please elaborate? – Fabio Jun 07 '17 at 15:40
  • @Fabio What is the error message or how it does not work? – Maxim Egorushkin Jun 07 '17 at 15:42
  • `$(BINDIR)/%$(OBJ) : %.cpp $(LIBHEADERS) Makefile objdir` `${PAPEROBJS} : $(PAPERHEADERS)` ` $(CC) -c $(CFLAGS) $< -o $@` – Fabio Jun 07 '17 at 16:09
  • Ok, can't get the format right. That is supposed to be 3 lines. It does not call the objdir phony target, which creates the bin directory. If I create the directory manually, then it error out trying to use the h file as main source for the o file. – Fabio Jun 07 '17 at 16:13
  • @Fabio You may like to post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Maxim Egorushkin Jun 07 '17 at 16:19
  • 1
    Just a note: you don't hvae to ensure that the rules come in any particular order or worry about prerequisite order. GNU make always uses the prerequisites of the rule that has the recipe first in the list, and any other prerequisites are added to the end of the list in the order they appear in the makefile. So `$<` will always be the first prerequisite in the rule containing the recipe (if there is one). – MadScientist Jun 08 '17 at 12:02
  • @MadScientist That is a nice feature. – Maxim Egorushkin Jun 08 '17 at 12:32