修改makefile列表以插入引号

时间:2015-07-13 22:17:39

标签: shell makefile quotes

在我的makefile中,我想从...中更改搜索目录列表

DIRS := ../project ../project/include ../project/src

类似以下内容...

INCLUDES := -I"../project" -I"../project/include" -I"../project/src"

我尝试在makefile中使用以下命令...

INCLUDES := $(patsubst %,-I"%",$(DIRS))

当makefile输出列表时,它不包含引号...

INCLUDES = -I../project -I../project/include -I../project/src

但是,当调用编译器时,makefile使用以下行...

g++ ... -I"../project" -I"../project/include" -I"../project/src"

我不明白。他们为什么不同?

1 个答案:

答案 0 :(得分:2)

你正确地做到了。它是由Makefile配方调用的shell,使引号消失。考虑一下Makefile:

DIRS     := ../project ../project/include ../project/src
INCLUDES := $(patsubst %,-I"%",$(DIRS))

$(info $(INCLUDES))

all:
        @echo $(INCLUDES)

make all返回:

-I"../project" -I"../project/include" -I"../project/src"
-I../project -I../project/include -I../project/src

如您所见,patsubst文本功能很好地完成了它的工作 由于外壳的可变扩展,报价已经消失 为了保留它们,你应该尝试转义它们:

INCLUDES := $(patsubst %,-I\"%\",$(DIRS))

在这种情况下,make all输出:

-I\"../project\" -I\"../project/include\" -I\"../project/src\"
-I"../project" -I"../project/include" -I"../project/src"