GNU Makefile substitution reference issue: using % twice

Viewed 55

I am trying to use GNU Make's substitution reference, but I need to refer to the % twice in the substitution. And it is not working the way I expected. Here is my Makefile:


foo := io protocol util
bar := $(foo:%=../Lib%/obj/lib%.a)

default: ; @echo bar = $(bar)

I want end up with:

bar = ../Libio/obj/libio.a ../Libprotocol/obj/libprotocol.a ../Libutil/obj/libutil.a

But this is what I end up with:

../Libio/obj/lib%.a ../Libprotocol/obj/lib%.a ../Libutil/obj/lib%.a```

I've read the GNU Make reference manual, and I don't see any mention or examples of using % twice in one statement. I don't even know if it is possible.

Any help would be greatly appreciated.

1 Answers

As you have discovered by reading the manual, it's not possible to use the pattern twice. Only the first is expanded: the rest are simply considered literal characters.

You can use the foreach function instead:

bar := $(foreach F,$(foo),../Lib$F/obj/lib$F.a)
Related