问题描述
在makefile中,使用\
转义换行符可以在多个源代码行之间拆分单行长字符串内容.但是,换行符替换为空格.源中是否存在不影响字符串内容的透明换行符?
In a makefile, escaping a new-line with \
allows to split a single-line long string content across multiple source lines. However, the new-line is replaced with a space. Is there a transparent line break in the source that does not affect the string content?
VAR=w\
o\
r\
d
all:
echo $(VAR)
所需的输出是'word',但实际的输出是'w o r d'.
The desired output is 'word', but the actual output is 'w o r d'.
推荐答案
最简单的解决方案是使用$\<newline>
拆分行(至少在使用GNU Make的情况下):
The simplest solution is to use $\<newline>
to split theline (at least if you are using GNU Make):
VAR = w$\
o$\
r$\
d
all:
echo $(VAR)
输出将是没有空格的单词".这是因为GNUMake将用单个替换backslash-newline-whitespace空格,使对VAR
的赋值等效于:
The output will be "word" with no spaces. This is because GNUMake will replace backslash-newline-whitespace with a singlespace, making the assignment to VAR
be equivalent to:
VAR = w$ o$ r$ d
来自 https://www.gnu.org/software/make/manual/html_node/Reference.html#Reference :美元符号后跟一个非美元符号的字符,开括号或开括号将单个字符视为变量名称."因此,$<space>
对是的扩展名称为单个空格字符的变量.从此变量默认情况下未定义,它将扩展为空字符串.
Fromhttps://www.gnu.org/software/make/manual/html_node/Reference.html#Reference:"A dollar sign followed by a character other than a dollar sign,open-parenthesis or open-brace treats that single character asthe variable name." So the $<space>
pairs are expansions ofthe variable whose name is a single space character. Since thisvariable is not defined by default, it will expand to the emptystring.
请注意,变量VAR
仍将包含$<space>
配对直到扩展.大多数时候,这没关系,但是如果您的makefile依赖于使用$(value VAR)
处理基础(未扩展)值,上述技术可能会提供令人惊讶的结果.
Note that the variable VAR
will still contain the$<space>
pairs until it is expanded. Most of the time, thisdoesn't matter, but if your makefile depends on using$(value VAR)
to process the underlying (unexpanded) value,the above technique may provide surprising results.
此外,最近发布的GNU Make 4.3现在明确记录了这一点.分割线的技术( https://www.gnu.org /software/make/manual/make.html#Splitting-Lines ):
Also, the recently released GNU Make 4.3 now explicitly documents thistechnique for splitting lines (https://www.gnu.org/software/make/manual/make.html#Splitting-Lines):
如果您需要分割一行,但又不想添加任何空格,则可以利用一个巧妙的技巧:用三个字符美元符号/反斜杠/换行符替换反斜杠/换行符对:
If you need to split a line but do not want any whitespace added, you can utilize a subtle trick: replace your backslash/newline pairs with the three characters dollar sign/backslash/newline:
var := one$\
word
make删除反斜杠/换行符并将以下行压缩为一个空格后,等效于:
After make removes the backslash/newline and condenses the following line into a single space, this is equivalent to:
var := one$ word
然后make将执行变量扩展.变量引用"$"指的是一个不存在一个字符名称"(空格)的变量,因此扩展为空字符串,并给出与以下内容等效的最终赋值:
Then make will perform variable expansion. The variable reference ‘$ ’ refers to a variable with the one-character name " " (space) which does not exist, and so expands to the empty string, giving a final assignment which is the equivalent of:
var := oneword
有关其他想法,请在此处查看我对类似问题的回答:如何在不带空格的Makefile中跨多行破坏变量定义?
For other ideas, see my answer to a similar question here:How can I break a variable definition across multiple lines in a Makefile without spaces?
可以在以下位置找到对行连续选项的更长时间的处理我的文章"GNU使行继续": http://drmikehenry.com/gnu-make-line-continuations/
A longer treatment of line continuation options can be found inmy article "GNU Make line continuations":http://drmikehenry.com/gnu-make-line-continuations/
这篇关于如何在不带空格的makefile中的行间打断字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!