为什么转义的变量$$和$不起作用 [英] Why does an escaped variable $$ work and not $

查看:111
本文介绍了为什么转义的变量$$和$不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在线程中问的上一个问题的后续问题:

This is a follow up question to the previous one I had asked in the thread:

搜索子目录并更新makefile中的路径变量

以下:

有一个规则makefile,它添加了各种标准定义, Include.mak.它具有一个定义:

There is a rules makefile which adds various standard defines, Include.mak. It has a define:

RESOLVED_PATH=$(subst $(abspath .)/,,$(abspath $(1)))

它正在另一个生成文件中使用:

It is being used in another makefile:

include Include.mak

ROOT:=.
define UPDATE_GUI
d:= $$(strip $(1))
GUI_FOLDER:= $(ROOT)/$(d)
GUI_ALL:=$(call RESOLVED_PATH,$(GUI_FOLDER)/$(d)_generated.txt)
$$(warning GUI_ALL is set to $$(GUI_ALL))
endef

TARGET_FILE:= ProjectParms.rb

ACTIVE_GUIS = $(wildcard GUI*/$(TARGET_FILE))
GUIS_OF_INTEREST = $(dir $(ACTIVE_GUIS))
GUIS= $(patsubst %/,%,$(GUIS_OF_INTEREST))
$(foreach GUI, $(GUIS), $(eval  $(call UPDATE_GUI, $(GUI))))

这不会提供所需的输出,但是如果我在行中将其展开:

This does not give the output as desired, but if I expand the same in the line:

GUI_ALL:=$$(subst $$(abspath .)/,,$$(abspath $$(GUI_FOLDER)/$$(d)_generated.txt)

$$(warning GUI_ALL is set to $$(GUI_ALL))

一切正常,有人可以告诉我为什么吗? $(调用RESOLVED_PATH,Param)已在整个make项目的不同位置使用(尽管其他任何地方都没有使用$(warning''),我只是用它来进行快速的完整性检查.

It works fine, can someone tell me why? The $(call RESOLVED_PATH, Param ) is being used throughout the make project in different places (though no $(warning '') is being used anywhere else, I am just using that for a quick sanity check.

推荐答案

您必须对函数中的所有内容进行转义,但实际的call参数除外.有时您不想要,但是几乎所有时间都想要.所以应该是:

You have to escape everything in the function except the actual call arguments. Sometimes you don't want this but almost all the time you do. So it should be:

define UPDATE_GUI
d := $$(strip $(1))
GUI_FOLDER := $$(ROOT)/$$(d)
GUI_ALL := $$(call RESOLVED_PATH,$$(GUI_FOLDER)/$$(d)_generated.txt)
$$(warning GUI_ALL is set to $$(GUI_ALL))
endef

对于调试,可以将eval函数替换为info函数;这将为您打印出eval可以使用的makefile语法.如果这样做的话,您会看到eval在获取要解析的文本之前就已经错了,因为call对其进行了扩展.例如,

For debugging, you can replace the eval function with the info function; this will print out for you what the makefile syntax is that eval would work on. If you do that, you'll see that by the time eval gets the text to parse it's already wrong, because call expanded it. E.g.,

$(foreach GUI,$(GUIS),$(info $(call UPDATE_GUI,$(GUI))))

ETA

我将指出,您可以通过完全删除call函数来大大简化此操作.为此,必须将UPDATE_GUI定义与foreach紧密绑定.因为无论如何只是将一个参数传递给call,所以不能这样做,而是直接将GUI变量(foreach中的循环变量)放入UPDATE_GUI定义中;那么您就不需要打电话了.像这样:

I will point out that you can greatly simplify this by removing the call function altogether. In order to do this you'll have to be OK with the UPDATE_GUI define being tightly bound with the foreach. Since you're just passing one argument to call anyway, you can just not do that and put the GUI variable (the loop variable in foreach) directly into the UPDATE_GUI define; then you don't need call. Like this:

define UPDATE_GUI
d := $(strip $(GUI))
GUI_FOLDER := $(ROOT)/$(d)
GUI_ALL := $(call RESOLVED_PATH,$(GUI_FOLDER)/$(d)_generated.txt)
$(warning GUI_ALL is set to $(GUI_ALL))
endef

$(foreach GUI,$(GUIS),$(eval $(UPDATE_GUI)))

这篇关于为什么转义的变量$$和$不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆