如何在不带空格的Makefile中跨多行破坏变量定义? [英] How can I break a variable definition across multiple lines in a Makefile without spaces?

查看:90
本文介绍了如何在不带空格的Makefile中跨多行破坏变量定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下Makefile

Consider the following Makefile

CP = .:${HADOOP_HOME}/share/hadoop/common/lib/hadoop-auth-2.2.0.jar:\
${HADOOP_HOME}/share/hadoop/hdfs/hadoop-hdfs-2.2.0.jar:\
${HADOOP_HOME}/share/hadoop/common/hadoop-common-2.2.0.jar:\
${HADOOP_HOME}/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.2.0.jar:\
${HADOOP_HOME}/share/hadoop/mapreduce/lib/hadoop-annotations-2.2.0.jar\

all:
    echo $(CP)

运行make的输出是

.:/home/hduser/Hadoop/hadoop-2.2.0/share/hadoop/common/lib/hadoop-auth-2.2.0.jar: /home/hduser/Hadoop/hadoop-2.2.0/share/hadoop/hdfs/hadoop-hdfs-2.2.0.jar: /home/hduser/Hadoop/hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar: /home/hduser/Hadoop/hadoop-2.2.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.2.0.jar: /home/hduser/Hadoop/hadoop-2.2.0/share/hadoop/mapreduce/lib/hadoop-annotations-2.2.0.jar

请注意,每个:之后都有空格.

Observe that there are spaces after each :.

是否可以通过换行符来定义变量CP,但没有多余的空格替换每条换行符?

Is there a way to define the variable CP with the line breaks, but without the extraneous space substituting every newline?

推荐答案

不可能防止反斜杠换行符变成空格,并且以后尝试删除空格是笨拙且容易出错的(如果应该有空格,该怎么办?),但是您可以在生成时将其删除.这具有在任何地方工作的显着优势,即使在内部函数调用中也是如此.诀窍是将产生的空间嵌入到可扩展为零的表达式中.

It’s impossible to prevent backslash-newline from becoming a space, and it’s clumsy and error-prone to try to remove the spaces afterwards (what if there are supposed to be spaces?), but you can remove each as it’s produced. This has the significant advantage of working anywhere, even inside function calls. The trick is to embed the space produced in an expression that expands to nothing.

$(call foo)具有空/未定义的foo可以使用,但我们可以做得更好:变量名称可以在(GNU)Make中包含空格.很难分配给他们,但是我们还是不想.因此,我们可以将其缩短为$(a b)甚至$(a );反斜杠换行符将在查找之前变成一个空格.但是,即使只有一个空格也可以:

$(call foo) with empty/undefined foo would work, but we can do better: variable names can contain spaces in (GNU) Make. It’s hard to assign to them, but we don’t want to anyway. So then we can shorten it to $(a b) or even $(a ); a backslash-newline will be turned into a space before the lookup. But even a single space works:

foo=bar$(\
)baz

最后,对于单字符变量名称,可以省略括号:

Finally, the parentheses may be omitted for a single-character variable name:

foo=bar$\
baz

…最终看起来我们(完全)在换行符之间进行了转义,而不是以某种方式使用它.只要没有一个人分配为 " (甚至比使用还要疯狂!).

…which finally looks like we are (fully) escaping the newline rather than using it somehow. So long as no one assigns to " " (which is even crazier than using it!), anyway.

这篇关于如何在不带空格的Makefile中跨多行破坏变量定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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