今天用Go语言开发过程中,执行测试用例发现如下错误:

go test -v ./services/

# 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'

https://github.com/golang/go/issues/38876 在这个上面找到了类似的问题

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

尝试里面说的 重新安装 xcode 命令行工具,并没有解决问题。

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

尝试修改CGO_CPPFLAGS变量后问题解决。

猜想可能是哪次 link 过一些头文件到 /usr/local/include/目录引起的。具体原因也不知道,不改再尝试了,可能把其它环境弄坏,暂时解决就好。

打赏
Golang test error: macro expansion producing ‘defined’ has undefined behavior [-Werror,-Wexpansion-to-defined]

发表评论