在 Makefile 的 foreach 循环中使用两个参数评估函数

Evaluate function with two arguments in foreach loop in Makefile

我需要在 foreach 循环中调用一个带有两个参数的函数

define obj-goal
: 
    gcc $(CXXFLAGS) -c $$< -o $$@
endef

函数的两个参数来自两个列表

C_FILES:=$(patsubst %.py,%.c,$(SRC_FILES))
OBJ_FILES:=$(patsubst %.py,build/temp.linux-x86_64-2.7/%.o,$(SRC_FILES))

目前我所做的是连接两个变量,然后使用两个函数 first-varlast-var 再次拆分,然后传递给所需的函数

first-var = $(firstword $(subst ^, ,))
last-var = $(lastword $(subst ^, ,))

OC_FLIST:=$(join $(OBJ_FILES),$(patsubst %,^%,$(C_FILES)))
$(foreach filecat,$(OC_FLIST),$(info $(call obj-goal,$(call first-var,$(filecat)),$(call last-var,$(filecat)))))

有更好的方法吗?喜欢:

$(foreach (ofile, cfile), ($(OBJ_FILES), $(C_FILES)) ,$(eval $(call obj-goal,$(ofile),$(cfile))))

有一种更简单的方法。使用 static pattern rule:

$(OBJ_FILES): build/temp.linux-x86_64-2.7/%.o: %.c
    gcc $(CXXFLAGS) -c $< -o $@ 

在您的特定情况下,Beta 的答案要好得多。

iterating/zipping两个列表的一般问题需要索引,恐怕$(shell seq):

LIST1 := a b c
LIST2 := 1 2 3

test:
    echo $(foreach INDEX, \
                   $(shell seq 1 $(words $(LIST1))), \
                   $(word $(INDEX), $(LIST1)) and $(word $(INDEX), $(LIST2)))