On Thu, 2021-12-02 at 17:12 +0000, Ian Molton wrote: > So presumably the "right" approach is to use submakefiles somehow, > but that brings a whole slew of questions about what is defined > where, and what applies to what, and I'm just finding myself > completely lost - for example - > > I tried to make a rule that would work for objects located in > subfolders. This is the closest I got, and it worked fine: > > $(OBJS): %.o: %.c > mkdir -p .deps/$(@D) > $(CC) $(CFLAGS) -c -o $@ $<
Your basic problem is you're trying to use static pattern rules. A static pattern rule is actually an explicit rule, so the above means you've created an explicit rule for every object in OBJS. Then if you later write: > $(OBJS): %.o: %.s > mkdir -p .deps/$(@D) > $(CC) $(ASFLAGS) -c -o $@ $< Now you're creating a different explicit rule for those same objects, with a different recipe and prerequisite. That can't work. You need to use normal implicit rules via a pattern rule: %.o: %.c mkdir -p .deps/$(@D) $(CC) $(CFLAGS) -c -o $@ $< %.o: %.s mkdir -p .deps/$(@D) $(CC) $(ASFLAGS) -c -o $@ $< Now make will build the .o from a .c file if one exists, else if no .c file exists it will build the .o from a .s file.