GNU makefile如何以及何时引用字符串 [英] GNU makefile how to and when to quote strings

查看:62
本文介绍了GNU makefile如何以及何时引用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以及何时在make文件中引用字符串?什么是最佳做法?

How and when do I quote a string in a make file? What is best practice?

以下是报价方式吗?

$(warning  $(shell ls -ld "$(CURDIR)" ) )

我对Bash熟悉,通常在Bash中引用变量以允许嵌入空格.您是否在makefile中这样做?

I'm familiar with Bash where you usually quote variables to allow for embedded spaces. Do you do such in a makefile?

我应该如何使用字符串进行赋值语句?

How should I do assignment statements with a string?

vara := "$(CURDIR)"

varb := $(CURDIR)

varc := /home/me/source

vard := "/home/me/source"

等号后的空格如何?

推荐答案

由于make ,您绝不能引用任何. Make无法以任何方式理解或解析单引号或双引号字符.您在makefile中编写的每个引号字符都将保留为文字引号,并按原样传递给make调用的命令.

You should never quote anything because of make. Make doesn't understand or parse single- or double-quote characters in any way. Every quoting character you write in a makefile will be kept as a literal quote and passed along as-is to the commands that make invokes.

因此,如果外壳程序期望并可以解释带引号的字符串,则应使用引号.如果外壳程序不希望或无法正确解释带引号的字符串,则不应使用引号.

So, if the shell expects and can interpret a quoted string, then you should use quotes. Where the shell doesn't expect or won't correctly interpret a quoted string, you should not use quotes.

在您的示例中,引号是否可接受取决于使用的方式.如上所述,make不会对引号做任何特殊的处理,这意味着vard(例如)包含文字字符串"/home/me/source"(包括引号).

In your examples, whether the quotes are acceptable or not depends on how those variables are used. As above, make won't do anything special with quotes, which means that vard (for example) contains the literal string "/home/me/source" (including the quotes).

如果您使用该值的方式由外壳程序为您处理引号,那么就可以了:

If you use that value in a way where the shell will handle the quotes for you, then it's fine:

all: ; echo $(vard)

将打印/home/me/source(不带引号),因为shell会解释它们.但是,如果您在make上下文中使用该变量,例如,将其用作目标或先决条件:

will print /home/me/source (no quotes) because the shell interprets them. But if you use the variable in a make context, for example as a target or a prerequisite:

all: $(vard)
$(vard): ; echo $@

那么这是正确的,因为目标和先决条件是文字字符串"/home/me/source"(包括引号).

then this is not right, because the target and prerequisite are the literal strings "/home/me/source" (including the quotes).

通常,最好不要在变量中的文件名周围使用引号,而应在配方中在make变量附近添加引号.当然,如果变量包含整个Shell脚本,而不仅仅是文件名,则应在脚本中添加适当的引号.

In general it's best to not use quotes around filenames in variables, and instead add the quotes in the recipe around the make variable. Of course if the variable contains an entire shell script, not just a filename, then you should add appropriate quoting to the script.

这篇关于GNU makefile如何以及何时引用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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