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

How to deal with special characters in make command expansion?

给定以下 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 函数之间表现不同。有人可以解释为什么,以及我如何修改脚本,以便它也可以与反引号命令扩展一起使用,如果可能的话?

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

$ 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")')"