I have a Makefile that has two identical targets, one for mkdir and the other for cp.

The Makefile creates dir when it does not exist, and then copy files. I wonder how it works. As I know, Makefile does not execute recipes line by line, from top to bottom.

Can someone explain how it works?

# Makefile
SRCDIR := ../../../src
SUBDIR := include

HEADERS := a.hpp \
HEADERS := $(addprefix $(SUBDIR)/, $(HEADERS))

# Copy headers in SRCDIR to SUBDIR
prebuild: $(HEADERS)


    mkdir $@

$(HEADERS): %: $(SRCDIR)/%
    cp $< $@


@cryptarch What I am doing is

$ make prebuild

What this Makefile should do is

copy ../../../src/include/* to ./include/

  • I suggest create a minimal working example (MWE). What is the smallest Makefile which exhibits the behaviour you're interested in, which anyone can run on their system? And what command are you using to "run" the Makefile? We don't have the same directory setup as you, and you might be using custom arguments to make, so we can't just take your example Makefile and try it out to see what you mean. – cryptarch Feb 4 at 18:38
  • I changed the tag since this is not a Makefile and the content thus cannot be ralated to make. – schily Feb 4 at 20:16
  • I can't find the two identical targets. Did you not paste them into the question? – ctrl-alt-delor Feb 4 at 20:52
  • Un-necessary copying of data, is almost always a bad idea. Consider alternatives: e.g. tell compiler where to find them. – ctrl-alt-delor Feb 4 at 20:58
  • @ctrl-alt-delor I think the duplicate is $(HEADERS). – Stephen Kitt Feb 4 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.