英文:
Makefile command substitution does not accept parameters
问题
我正在尝试为我的go
项目创建一个简单的Makefile
。
以下命令替换似乎引发了以下问题:
GO_BUILD := 'go build -ldflags "-s -w" -a -installsuffix cgo'
.PHONY: backup
build-cli:
@$(GO_BUILD) -o cli ./cli
看起来创建了以下问题:
▶ make build-cli
make: go build -ldflags "-s -w" -a -installsuffix cgo: No such file or directory
make: *** [build-cli] Error 1
在语法上,正确的替换方式是什么?
英文:
I am trying to create a simple Makefile
for my go
project.
The following command substitution
GO_BUILD := 'go build -ldflags "-s -w" -a -installsuffix cgo'
.PHONY: backup
build-cli:
@$(GO_BUILD) -o cli ./cli
seems to create the following problem
▶ make build-cli
make: go build -ldflags "-s -w" -a -installsuffix cgo: No such file or directory
make: *** [build-cli] Error 1
What is the syntactically correct way of substituting go build -ldflags "-s -w" -a -installsuffix cgo
?
答案1
得分: 2
从变量中删除多余的引号应该可以解决这个问题:
GO_BUILD := go build -ldflags "-s -w" -a -installsuffix cgo
否则,shell(由make
生成)将看到这个命令行:
'go build ...' -o cli ./cli
它会正确地将整个字符串go build ...
视为argv[0]
,并尝试将其作为可执行文件找到。
英文:
Removing the superfluous quotes from the variable should do it:
GO_BUILD := go build -ldflags "-s -w" -a -installsuffix cgo
Otherwise, the shell (that make
spawns) sees this command line:
'go build ...' -o cli ./cli
It correctly treats the whole string go build ...
as argv[0]
and tries to find it as an executable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论