Makefile with symbols confusion

Viewed 21

I have makefile and I'm confused about what this command does

build:
    mkdir -p $@

%: build/%
    cp $< $@

Can any body explain it?

1 Answers

The makefile defines two rules: build and a pattern rule. The build rule simply creates a directory called build (note: $@ expands to the name of the target -- see Automatic Variables).

The second rule is a pattern rule. The pattern here is %, which matches anything. That target is dependent on build/% where the % is the target name. (so foo would be dependent on build/foo for example). If the dependency exists or can be built, foo would be created by copying the first dependency ($< expands to the first dependency which would be build/foo) to foo.

Related