简体   繁体   English

c ++ makefile将prefeix附加到带空格的双引号项列表中

[英]c++ makefile append prefeix to list of double-quoted items with spaces

Lets say I have this in my makefile: 让我说我的makefile中有这个:

FOLDERS = "C:\Program Files\some3rdpartytool\inc\thing1.h" \
          "C:\Program Files\some3rdpartytool\lib\libthing.lib" \
          "C:\data\data1.h" \

Now I want INCLUDE to contain: 现在我希望INCLUDE包含:

INCLUDE = -I"C:\Program Files\some3rdpartytool\inc" \
          -I"C:\Program Files\some3rdpartytool\lib" \
          -I"C:\data" \

Normally this is easy - you just to something like: 通常这很容易 - 你只需要:

INCLUDE = $(addprefix -I, $(FOLDERS))
# you can also use the standard makefile function to take the folder path
# only - I just forget what it is off the top of my head...

But no matter what I do, this always only works on white space separated lists (as it is designed to) so I get something like this: 但无论我做什么,这总是只适用于空白分隔列表(因为它的设计),所以我得到这样的东西:

INCLUDE = -I"C:\Program \
          -IFiles\some3rdpartytool\inc" \
          -I"C:\Program \
          -IFiles\some3rdpartytool\lib" \
          -I"C:\data" \

Is there a standard makefile way to do this? 有没有标准的makefile方法来做到这一点? (standard == portable), I want this to work on windows and Linux ideally. (标准==便携式),我希望这适用于Windows和Linux。

Note I did think about replacing any ' "' --> ' -I"' but I can't seem to use a white space in the subst command... I am struggling on a way forward with this... 注意我确实考虑过替换任何''' - >' - I''但我似乎无法在subst命令中使用空格...我正在努力寻找前进的道路......

All right, here we go. 好的,我们走了。

FOLDERS = "C:\Program Files\blah\thing1.h" \
          "C:\Program Files\blah\libthing.lib" \
          "C:\data\data1.h"

Now we use I trick I learned from @MadScientist, defining a variable that contains a space, and using a placeholder that you're confident does not occur in your paths, such as "SPACE". 现在我们使用我从@MadScientist学习的技巧,定义一个包含空格的变量,并使用一个您确信不会在路径中出现的占位符,例如“SPACE”。

E :=
S := $E $E

X1 := $(subst $(S),SPACE,$(FOLDERS))
# "C:\ProgramSPACEFiles\blah\thing1.h"SPACE"C:\ProgramSPACEFiles\...

This seems to replace all instances of ' ', including the ones between paths, so we'll change those back: 这似乎取代了''的所有实例,包括路径之间的实例,所以我们将更改它们:

X2 := $(subst "SPACE","$(S)",$(X1))
# "C:\ProgramSPACEFiles\blah\thing1.h" "C:\ProgramSPACEFiles\...

Then add the "-I": 然后添加“-I”:

X3 := $(addprefix -I,$(X2))
# -I"C:\ProgramSPACEFiles\blah\thing1.h" -I"C:\ProgramSPACEFiles\...

Then change "SPACE" back to ' ': 然后将“SPACE”改回'':

X4 := $(subst SPACE,$(S),$(X3))
# -I"C:\Program Files\blah\thing1.h" -I"C:\Program Files...

@Beta's solution is nice and general. @ Beta的解决方案很好而且很通用。 Another option that is more specific but maybe simpler is to use something like: 另一个更具体但可能更简单的选项是使用类似的东西:

FOLDERS = "C:\Program Files\some3rdpartytool\inc" \
          "C:\Program Files\some3rdpartytool\lib" \
          "C:\data"

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

but of course this fails if you have folders outside of the C: drive. 但是,如果您在C:驱动器之外有文件夹,那么当然会失败。

A final option which is more general but still has a few issues (it reduces whitespace is the main one, but I've never heard of paths with multiple consecutive spaces so maybe that's OK) would be something like: 最后一个选项更通用,但仍然有一些问题(它减少了空白是主要的一个,但我从来没有听说过多个连续空格的路径,所以也许没关系)会是这样的:

INCLUDES := -I$(subst " "," -I",$(strip $(FOLDERS)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM