make变量中的反斜杠 - 换行符?

时间:2015-05-07 11:29:24

标签: makefile gnu-make

我有一个类似这样的makefile:

define finder
    find /usr/include /usr/local/include -name '*.h' \
    | xargs grep -in stupid
endef

all:

find:
    ${finder}

xfind:
    find /usr/include /usr/local/include -name '*.h' \
    | xargs grep -in stupid

目录列表实际上要长得多,这就是为什么我想在执行时打破这一行。 xfind完全符合我的要求:

$ make xfind
find /usr/include /usr/local/include -name '*.h' \
| xargs grep -in stupid

但是,我想使用canned recipe将其用于不同的目标;就像finder目标中使用的find一样。很遗憾,finder是一个变量,而不是recipe,因此different rules适用于它。处方执行后,所有内容都在一行中。

$ make find
find /usr/include /usr/local/include -name '*.h' | xargs grep -in stupid

有没有办法将反斜杠换行符嵌入变量中,直到它被用作配方?

1 个答案:

答案 0 :(得分:3)

实际上, 可以将反斜杠换行符嵌入到变量中。棘手的是反斜杠(花了一些时间才找到答案)。

# variable containing a newline
# there must be two blank lines between the define and endef
# (http://stackoverflow.com/a/17055840/2064196)
define nl


endef

# variable containing a backslash
# https://www.netbsd.org/docs/pkgsrc/makefile.html#makefile.variables
# backslash !=           echo "\\"
# the version below avoids $(shell), as suggested by bobbogo's comment
backslash := \$(strip)

现在罐头食谱可以写成

define find
find /usr/include /usr/local/include -name '*.h' ${backslash}${nl} \
| xargs grep -in stupid
endef

,输出是所需的

find /usr/include /usr/local/include -name '*.h' \
 | xargs grep -in stupid