我需要在foreach循环中调用带有两个参数的函数
define obj-goal
$1: $2
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-var
和last-var
再次分割,然后传递给所需的函数
first-var = $(firstword $(subst ^, ,$1))
last-var = $(lastword $(subst ^, ,$1))
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 $@