GNU Make动态目标中for循环失效问题求助
解决GNU Make 3.82中define宏里shell循环变量未展开的问题
问题根源:Make会将命令行中$i这类单字符变量引用视为Make变量展开,若未定义该变量则会被替换为空,导致shell执行echo ;输出空行。
解决方法1:改用多字符循环变量名
将shell循环中的单字符变量i改为多字符名称(如item),同时用$${item}传递给shell,避免Make误解析:
MAKEFLAGS += -s SHELL := /bin/bash define create_target $(1): echo $(1); \ echo $(2); \ for item in $(2); do \ echo $${item}; \ done endef $(eval $(call create_target, test, 1 2 3 4 5))
执行make test后输出:
test 1 2 3 4 5 1 2 3 4 5
解决方法2:用bash -c包裹循环命令
利用单引号阻止Make解析循环内部的$变量,通过bash子进程执行循环:
MAKEFLAGS += -s SHELL := /bin/bash define create_target $(1): echo $(1); \ echo $(2); \ bash -c 'for i in $(2); do echo $$i; done' endef $(eval $(call create_target, test, 1 2 3 4 5))
执行后同样能得到正确的遍历输出。
内容的提问来源于stack exchange,提问作者Heimish
相关产品推荐
相关产品推荐

