MacOS Catalina升级后,Go程序将无法运行

时间:2020-10-01 04:26:52

标签: macos go macos-catalina

这可能是一个奇怪的问题,但是在MacOS Catalina升级后,某些用Go编写的程序将无法在我的MacBook Pro上运行。

尽管运行了基本的“ Hello World”程序,但是随后我使用net/http包编写程序,却遇到以下错误:

注意:这些程序是正确的,它们在Docker容器中可以正常运行

输出( IntelliJ ):


# runtime/cgo
In file included from gcc_darwin_amd64.c:6:
/usr/local/include/pthread.h:331:6: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]
/usr/local/include/pthread.h:200:2: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
/usr/local/include/pthread.h:331:6: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]
/usr/local/include/pthread.h:200:34: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
/usr/local/include/pthread.h:540:6: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]
/usr/local/include/pthread.h:200:2: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
/usr/local/include/pthread.h:540:6: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]
/usr/local/include/pthread.h:200:34: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'

我已经尝试过的事情:

  1. 重新安装go
  2. 重新安装xcode
  3. 检查GOPATHGOROOT的设置是否正确

使用过:

MacOS version - Catalina 10.15.6
Go version - go1.15.2 darwin/amd64

2 个答案:

答案 0 :(得分:0)

在终端窗口中输入xcode-select -print-path并检查您的安装目录。在我的情况下,输出为:/Applications/Xcode.app/Contents/Developer

如果是相同情况,则在终端上使用以下命令可以解决问题:

sudo xcode-select --switch /Library/Developer/CommandLineTools

另一种选择是使用issue中提到的CGO_CPPFLAGS,但是它可以在会话中使用:

export CGO_CPPFLAGS="-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wno-builtin-requires-header"

答案 1 :(得分:0)

遗憾的是,所有试用都没有奏效。

我已经使用 IntelliJ IDEA Ultimate 有一段时间了..所以我尝试使用 IDE 设置 Go SDK、GOROOT 和 GOPATH(我想到了一个想法)。

我在 MacBook 上卸载/删除了与 Go 相关的所有内容。

然后我在IntelliJ IDEA上安装了Go插件,果然提示Go SDK不可用,GOROOT和GOPATH都没有设置。

我按照说明进行操作,剩下的交给 IntelliJ IDEA!

它下载并安装了 Go SDK,让我选择 GOPATH,它索引了东西,现在一切都像魅力一样工作!