0
votes

In below snippet of Makefile the normal way of makefile behave is when foo.o has dependency listed here like /tmp/foo.o: abc.o xyz.o lmn.o etc then while parsing make goes to the rule which list how to make the dependency (abc.o xyz.o lmn.o) but here the target /tmp/foo.o has listed no dependency even then how the next line executed or it simply ignored.

Please explain this as soon as possible that how and why that line (%.o: $$(addsuffix /%.c,foo bar) foo.h) executed. Any body help is highly appreciated . I need it because of my lecture on make file.

.SECONDEXPANSION:

/tmp/foo.o:

%.o: $$(addsuffix /%.c,foo bar) foo.h @echo $^

1

1 Answers

1
votes

This is a peculiar makefile. It appears that the person who wrote it does not understand Make very well.

The use of SECONDEXPANSION causes the prerequisite list:

$$(addsuffix /%.c,foo bar) foo.h

to be expanded twice; first to this:

$(addsuffix /%.c,foo bar) foo.h

and then to this:

foo/%.c bar/%.c foo.h

This use of SECONDEXPANSION accomplishes nothing. We could just as well write the rule like this:

%.o: $(addsuffix /%.c,foo bar) foo.h
    @echo $^

or this:

%.o: foo/%.c bar/%c foo.h
    @echo $^

In any case, this is a pattern rule. If you attempt to build snaf.o (and there is no explicit rule for the target snaf.o), then Make will consider this rule, matching the stem snaf, so that the rule is equivalent to this:

snaf.o: foo/snaf.c bar/snaf.c foo.h
    @echo $^

None of this has any bearing on the target /tmp/foo.o, since the pattern rule will not match a target that has a slash in it. (If the pattern rule were /tmp/%.o: ..., that would be a different story.) So the two rules do not really interact, and the author's intention is unclear.