Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

gnu make - GNU makefile how to and when to quote strings

How and when do I quote a string in a make file? What is best practice?

Is the following the way to quote?

$(warning  $(shell ls -ld "$(CURDIR)" ) )

I'm familiar with Bash where you usually quote variables to allow for embedded spaces. Do you do such in a makefile?

How should I do assignment statements with a string?

vara := "$(CURDIR)"

varb := $(CURDIR)

varc := /home/me/source

vard := "/home/me/source"

What about the space after the equal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should never quote anything because of make. Make doesn't understand or parse single- or double-quote characters in any way. Every quoting character you write in a makefile will be kept as a literal quote and passed along as-is to the commands that make invokes.

So, if the shell expects and can interpret a quoted string, then you should use quotes. Where the shell doesn't expect or won't correctly interpret a quoted string, you should not use quotes.

In your examples, whether the quotes are acceptable or not depends on how those variables are used. As above, make won't do anything special with quotes, which means that vard (for example) contains the literal string "/home/me/source" (including the quotes).

If you use that value in a way where the shell will handle the quotes for you, then it's fine:

all: ; echo $(vard)

will print /home/me/source (no quotes) because the shell interprets them. But if you use the variable in a make context, for example as a target or a prerequisite:

all: $(vard)
$(vard): ; echo $@

then this is not right, because the target and prerequisite are the literal strings "/home/me/source" (including the quotes).

In general it's best to not use quotes around filenames in variables, and instead add the quotes in the recipe around the make variable. Of course if the variable contains an entire shell script, not just a filename, then you should add appropriate quoting to the script.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...