A few implicit rules for prerequisites for order only

I am trying to pre-order just to make sure that the assembly directory exists before the output of the files. According to the GNU Make manual :

you can still declare several lines of prerequisites for the same purpose: they are added accordingly (the usual preconditions are added to the list of usual preconditions, only the preconditions for the order are added to the list of preconditions for the order only).

So I expect this to work:

BUILDDIR:=build

all: $(BUILDDIR)/test.o
clean:
    rm -rf $(BUILDDIR)

$(BUILDDIR):
    mkdir -p $(@)

$(BUILDDIR)/%.o: | $(BUILDDIR)
$(BUILDDIR)/%.o: %.c
    gcc -o $@ -c $<

But this is not so, the directory could not be created. However, when I set the rule on one line, it works:

$(BUILDDIR)/%.o: %.c | $(BUILDDIR)

, , make , , . ?

, :

$ make -v
GNU Make 3.81
+4
1

, - cancel . , $(BUILDDIR)/%.o: | $(BUILDDIR) .

, :

BUILDDIR := build
OBJS     := $(BUILDDIR)/test.o

all: $(OBJS)
clean:
    $(RM) -r $(BUILDDIR)

$(BUILDDIR):
    mkdir -p $@

$(OBJS): | $(BUILDDIR)
$(OBJS): $(BUILDDIR)/%.o: %.c
    $(CC) -o $@ -c $<
+2

Source: https://habr.com/ru/post/1616604/


All Articles