如何在make命令扩展中处理特殊字符?

时间:2015-02-13 13:27:47

标签: python makefile escaping

给出以下python脚本:

print("include_path_with\\[weird\\]characters")

和以下makefile

main:
        g++ main.cpp -I`python script.py`

假设main.cpp实际上包含来自include_with\\[weird\\]characters的文件,这对我来说失败了,编译器无法找到包含的文件。

但是,如果我改用shell function,它就可以了。如果路径不包含奇怪的字符,它也可以工作。因此,出于某种原因,在反引号和shell - 函数之间转义似乎表现不同。有人可以解释为什么,以及我如何修改脚本以便它也可以使用反引号命令扩展,如果可能的话?

我在bash shell中的Mac OS X 10.10.2上使用GNU Make 3.81。该脚本使用Python 3.4.2运行。

1 个答案:

答案 0 :(得分:1)

$ cat BP.mk
VAR := $(shell python -c 'print("include_path_with\\[weird\\]characters")')

all:
        echo 'DIRECT := `python -c '\''print("include_path_with\\[weird\\]characters")'\''`'
        echo "DIRECT := `python -c 'print("include_path_with\\[weird\\]characters")'`"
        echo DIRECT := `python -c 'print("include_path_with\\[weird\\]characters")'`
        echo 'VAR := $(VAR)'
        echo "VAR := $(VAR)"
        echo VAR := $(VAR)
$ make -f BP.mk
echo 'DIRECT := `python -c '\''print("include_path_with\\[weird\\]characters")'\''`'
DIRECT := `python -c 'print("include_path_with\\[weird\\]characters")'`
echo "DIRECT := `python -c 'print("include_path_with\\[weird\\]characters")'`"
DIRECT := include_path_with\[weird\]characters
echo DIRECT := `python -c 'print("include_path_with\\[weird\\]characters")'`
DIRECT := include_path_with\[weird\]characters
echo 'VAR := include_path_with\[weird\]characters'
VAR := include_path_with\[weird\]characters
echo "VAR := include_path_with\[weird\]characters"
VAR := include_path_with\[weird\]characters
echo VAR := include_path_with\[weird\]characters
VAR := include_path_with[weird]characters

请注意在所有情况下,但反斜杠持续存在于输出中的最后一个?那就是问题所在。你不希望他们在那里。所以你想要的不是打印它们然后引用扩展,所以shell根本不处理结果。

所以要么

VAR2 := $(shell python -c 'print("include_path_with[weird]characters")')
g++ main.cpp -I'$(OUT)'

g++ main.cpp -I"$$(python -c 'print("include_path_with[weird]characters")')"