当构建参数包含 `-gcflags"all=-N -l"` 并在 MacOS 中导入 net/http 时构建失败

Build fails when build parameters contain `-gcflags"all=-N -l"` and import net/http In MacOS

当我尝试调试时,go build 参数包含 -gcflags "all=-N -l",我导入 net/http,然后构建将失败。

我尝试使用 brew 重新安装 golang,但没有用。

卸载后用offical installation试过,不行。

我试了 sudo mv /Library/Developer/CommandLineTools CommandLineTools.old && xcode-select --install,没用。

在 JimB 的建议下,我尝试了 go env -w CGO_ENABLED=0,以便我可以正确构建。

有什么建议吗?

构建失败消息:

# crypto/x509
In file included from /usr/local/Cellar/go/1.14/libexec/src/crypto/x509/root_cgo_darwin.go:17:
In file included from /System/Library/Frameworks/Security.framework/Headers/Security.h:31:
/System/Library/Frameworks/Security.framework/Headers/SecItem.h:452:49: error: expected ','
    API_AVAILABLE(macos(10.7), ios(NA), bridgeos(NA));
                                                ^
    ......
                                                ^
In file included from /usr/local/Cellar/go/1.14/libexec/src/crypto/x509/root_cgo_darwin.go:17:
In file included from /System/Library/Frameworks/Security.framework/Headers/Security.h:35:
/System/Library/Frameworks/Security.framework/Headers/SecImportExport.h:317:55: error: expected ','
          API_AVAILABLE(macos(10.7), ios(NA), bridgeos(NA));
                                                      ^
    ......
/System/Library/Frameworks/Security.framework/Headers/SecImportExport.h:656:49: error: expected ','
    API_AVAILABLE(macos(10.7), ios(NA), bridgeos(NA));
                                                ^
    ......

27 errors generated.
package main
import "os"
func main() {
    println(os.Args)
}
> go build main.go                       # success
> go build -gcflags "all=-N -l" main.go  # success

--------------------------------------

package main
import "net/http"
func main() {
    println(http.DefaultClient)
}
> go build main.go                       # success
> go build -gcflags "all=-N -l" main.go  # fail       <-----------

MacOS version: 10.13.6

Xcode version: 10.0

Go version 1.14

> go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/XXX/Library/Caches/go-build"
GOENV="/Users/XXX/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/XXX/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build994818786=/tmp/go-build -gno-record-gcc-switches -fno-common"

我发现一些类似的问题:Go: build error when compiling unit tests (I'm trying to mock influxdb types)

但是我没有使用相对导入

  1. go env -w CGO_ENABLED=0可以暂时解决这个问题
  2. 升级MacOS和XCode后,此问题未再出现。

现在我的版本:

MacOS 10.15.3

XCode 11.3.1(11C504)