0

Lets say I had the following two rules:

# C++ source file compilation
$(BIN)/%.o: $(SRC)/%.cpp
    @$(MKDIR) $(BIN)
    @printf "Compiling ${CYAN}$<${NC}\r\n"
    @$(CPP) $(CPPFLAGS) -I$(INC) -o $@ $<

$(BIN)/%.o: $(SRC)/*/%.cpp
    @$(MKDIR) $(BIN)
    @printf "Compiling ${CYAN}$<${NC}\r\n"
    @$(CPP) $(CPPFLAGS) -I$(INC) -o $@ $<

They only vary by the additional directory wildcard in the prerequisites. How do I collapse them into a single rule that handles the whole directory subtree at $(SRC)/

Frank Miller
  • 499
  • 1
  • 7
  • 19

1 Answers1

0

The simplest thing to do is use VPATH for this:

VPATH = $(SRC) $(wildcard $(SRC)/*/.)

$(BIN)/%.o: %.cpp
        @$(MKDIR) $(BIN)
        @printf "Compiling ${CYAN}$<${NC}\r\n"
        @$(CPP) $(CPPFLAGS) -I$(INC) -o $@ $<

I urge you to avoid adding @ before all your recipe lines; it leads to a real inability to debug issues.

MadScientist
  • 92,819
  • 9
  • 109
  • 136