Makefile命令替换不接受参数。

huangapple go评论77阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年8月6日 16:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73258381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定