Makefile 中的变量扩展

问题描述

Make 似乎仅在规则处于活动状态后扩展变量。

make ok 显示解析后的名称(即“test”)

make bad 声称依赖于“grep”(而不是“test”)

我怎样才能让 make 在检查“坏”规则的依赖关系之前展开 NAME?

CMakeLists.txt

add_executable(test testA.c testB.c)  # compiler (output input input ...)

Makefile(完整):

#========================================
# Shell cannot handle parentheses:
#    *** unterminated call to function 'shell': missing ')'.  Stop.
# Have also tried putting {"(",")","\(","\)"} in variables
#   ...but it just created a different problem
#
#NAME := $(shell grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/')
  
#========================================
# Curious results (same with or without the colon)
#
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
    
#========================================
# As expected,displays:
#    NAME=¦test¦"
#
PHONY: ok
ok:
    @echo "NAME=¦${NAME}¦"
 
#========================================
# Dies with:
#    *** No rule to make target '`grep',needed by 'bad'.  Stop."
#
.PHONY: bad
bad: ${NAME}
    @echo "NAME=¦${NAME}¦"

Makefile(相同,但精简):

NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
ok:
    @echo "NAME=¦${NAME}¦"
bad: ${NAME}
    @echo "NAME=¦${NAME}¦"

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)