假设我想将位于 中的 C++ 项目编译为/home/me/Google Drive/Foobar/
名为 的可执行文件Foobar
,并且我想使用 GNU Make 以使该过程自动化。
这是我的 Makefile 的样子:
OUTPUT = $(notdir $(CURDIR))
all $(OUTPUT):
g++ *.cpp -o $(OUTPUT)
Run Code Online (Sandbox Code Playgroud)
问题是,由于项目路径中有空格,因此该notdir
命令正在解释两个单独的路径并返回Google Foobar
。
我试过在$(CURDIR)
( $(notdir "$(CURDIR)")
)周围加上引号,但出现以下错误:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
我不明白问题出在哪里。
另外,我希望得到一个不涉及更改路径名的答案......谢谢:)
您可以尝试替换中的space
,然后获取. 像下面这样_
$(CURDIR)
$(OUTPUT)
null :=
SPACE := $(null) $(null)
OUTPUT = $(notdir $(subst $(SPACE),_,$(CURDIR)))
all $(OUTPUT):
g++ *.cpp -o $(OUTPUT)
Run Code Online (Sandbox Code Playgroud)
所以基本上之后string substitution (subst)
$(OUTPUT) will be Foobar
Run Code Online (Sandbox Code Playgroud)