Go 1.18在Mac上的构建错误:"unix/syscall_darwin.1_13.go:25:3: //go:linkname must refer to declared function or variable"

回答 4 浏览 3.5万 2022-03-17

我在Mac 12+上升级到了Go 1.18。

'go build'失败了,出现了类似的错误。

# golang.org/x/sys/unix
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/syscall_darwin.1_13.go:25:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.1_13.go:27:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.1_13.go:40:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:28:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:43:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:59:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:75:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:90:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:105:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3: 
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3: 
too many errors

Compilation finished with exit code 2
Walter Sun 提问于2022-03-17
4 个回答
#1楼
得票数 323

这是由golang.org/x/sys的旧版本引起的(在这个问题中提到)。修复方法是用以下方法更新。

go get -u golang.org/x/sys
Brits 提问于2022-03-17
blackgreen 修改于2022-05-25
我已经运行了go get -u golang.org/x/sys,但我的构建仍然失败了,出现了上述错误。/go/pkg/mod/golang.org/x/sys@v0.0.0-20201112073958-5cba982894dd/unix/syscall_darwin.1_13.go:29:3://go:linkname必须是指已声明的函数或变量Chandra 2022-04-22
有什么永久的解决办法吗?似乎我开始的每个项目都要这样做。Someone Special 2022-06-15
'go get' is no longer supported outside a module. 有谁知道最新的解决方案吗?Under-qualified NASA Intern 2022-06-20
参考 go get 在模块之外不再被支持--见发行说明 "在GOPATH模式下(GO111MODULE=off),go get仍然像以前一样构建和安装软件包。" 。因此,如果你不使用模块,请设置GO111MODULE=off(无论如何你都需要这样做才能构建)。Brits 2022-06-20
@Brits 谢谢你分享这个解决方案。这节省了我几个小时的调试时间。Purusottam 2022-09-21
#2楼
得票数 6

正如文档中提到的那样。

  1. 最简单的方法是:运行get -u golang.org/x/sys
  2. 手动:git克隆版本库到$GOPATH/src/golang.org/x/sys
Henry S. 提问于2022-05-03
#3楼
得票数 3

我也遇到了同样的错误,我做了以下的修正。

首先,我运行:

go get -u golang.org/x/sys

更新x/sys库。

然后,它打印了一些重要的信息。

go: downloading golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e
go: upgraded golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd => v0.0.0-20220704084225-05e143d24a9e

有了这一点,我去了go.mod并放置了以下一行(在go 1.14行之前)。

replace golang.org/x/sys => golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e

最后,我运行:

go mod vendor

然后,它更新了所有的库。

go: downloading github.com/hashicorp/go-cleanhttp v0.5.1
go: downloading github.com/hashicorp/go-rootcerts v1.0.0
go: downloading github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
go: downloading github.com/mitchellh/cli v1.0.0
[...]
go: downloading github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
go: downloading github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
go: downloading github.com/Azure/go-autorest/autorest/to v0.4.0

然后它就好了

❯ go install .
❯ terraform --version
Terraform v0.13.5

编辑:我在这里使用的是modules,如果你的情况不是这样,请设置GO111MODULE=off

Ricardo Silva 提问于2022-07-06
Ricardo Silva 修改于2022-07-06
#4楼
得票数 2

如果go get -u golang.org/x/sys不起作用,请确保在你的go.mod中删除这行:

replace golang.org/x/sys => golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6
Stéphane Bruckert 提问于2022-05-25
go.mod中没有提到这一行,go get命令也没有解决。还有什么可以做的?Vijayant 2022-05-28
标签