GNU make: list of object files from list of sources taken from pattern matched variable

gnu-make

Solution

Use static pattern rule:

prog_objs = $($*_SRCS:.c=.o)

.SECONDEXPANSION:
$(ALL_PROGS) : %_prog : $$(prog_objs)
    gcc - blah blah

Problem

So I have a variable: ``` PROGS = element0 \ element1 ``` I have variables for each one: ``` element0_SRCS = src/xxx.c src/q.c element1_SRCS = src/xxx.c src/z.c ``` If I have another set of variables then I can do what I want rather easily: ``` element0_OBJS = src/xxx.o src/q.o element1_OBJS = src/xxx.o src/z.o ALL_PROGS = $(foreach p, $(PROGS), $(p)_prog) all : $(ALL_PROGS) .SECONDEXPANSION: %_prog : $$($$*_OBJS) gcc - blah blah ``` However, I want to eliminate the need of the "_OBJS" vars and use the _SRCS ones. I can do this individual with each prog: ``` element0_prog : $(element0_SRCS:.c=.o) ``` Various attempts to use second expansion to recreate the %_prog rule though have failed. `$$($$*_SRCS:.c=.o)` ==> target pattern contains no '%' `$$(patsubst %.c,%.o,$$($$*_SRCS))` ==> builds the target with .c files instead of .o files. Pretty much at a loss here.

Original source