go: runtime/cgo: macro expansion producing 'defined' has undefined behavior

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

OS: macOS Catalina 10.15.4

go env Output
$ go env

GO111MODULE=“” GOARCH=“amd64” GOBIN=“” GOCACHE=“/Users/sr/Library/Caches/go-build” GOENV=“/Users/sr/Library/Application Support/go/env” GOEXE=“” GOFLAGS=“” GOHOSTARCH=“amd64” GOHOSTOS=“darwin” GOINSECURE=“” GONOPROXY=“” GONOSUMDB=“” GOOS=“darwin” GOPATH=“/Users/sr/go” GOPRIVATE=“” GOPROXY=“https://proxy.golang.org,direct” GOROOT=“/usr/local/go” GOSUMDB=“sum.golang.org” GOTMPDIR=“” GOTOOLDIR=“/usr/local/go/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=/var/folders/7t/mh0c8z9x6rn72vfl7jxrqh4r0000gn/T/go-build284435102=/tmp/go-build -gno-record-gcc-switches -fno-common”

What did you do?

Try to run this code:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", indexHandler)
	log.Fatal(http.ListenAndServe(":9999", nil))
}

// handler echoes r.URL.Path
func indexHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "URL.Path = %q\n", req.URL.Path)
}

What did you expect to see?

Expect the program to run.

What did you see instead?

# 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:332:5: error: declaration of built-in function 'pthread_create' requires inclusion of the header <pthread.h> [-Werror,-Wbuiltin-requires-header]
/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'

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (4 by maintainers)

Most upvoted comments

Hi! I already encountered an error like that one. I’ve had to add CGO_CPPFLAGS=-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wbuiltin-requires-header to my env to make it work. I think that it’s just removing the compilation flags.

My program is the same than yours and I was able to make it work using this. I’m using MacOs Catalina 10.15.4

This works for me except -Wbuiltin-requires-header -> -Wno-builtin-requires-header. Then, what add to env is: CGO_CPPFLAGS=-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wno-builtin-requires-header

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

Then check your environment via go env.

Thank you all for your help. I finally found out that my Xcode command line tools can not work as expectation. What I did is to run these lines in Terminal.

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

Hi! I already encountered an error like that one. I’ve had to add CGO_CPPFLAGS=-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wbuiltin-requires-header to my env to make it work. I think that it’s just removing the compilation flags.

My program is the same than yours and I was able to make it work using this. I’m using MacOs Catalina 10.15.4

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

Then check your environment via go env.

I met the same issue when using gvm to install go1.16 in Big Sur. Your solution totally works!

mac@macdeMac-mini ~ % gvm install go1.16

Downloading Go source...

Installing go1.16...
 * Compiling...
ERROR: Failed to compile. Check the logs at /Users/mac/.gvm/logs/go-go1.16-compile.log
ERROR: Failed to use installed version
mac@macdeMac-mini ~ %
mac@macdeMac-mini ~ % cat /Users/mac/.gvm/logs/go-go1.16-compile.log
Building Go cmd/dist using /usr/local/go. (go1.17.1 darwin/amd64)
Building Go toolchain1 using /usr/local/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for darwin/amd64.
# 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'
go tool dist: FAILED: /Users/mac/.gvm/gos/go1.16/pkg/tool/darwin_amd64/go_bootstrap install -gcflags=all= -ldflags=all= std cmd: exit status 2
mac@macdeMac-mini ~ % export CGO_CPPFLAGS="-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wno-builtin-requires-header"
mac@macdeMac-mini ~ % gvm install go1.16

Installing go1.16...
 * Compiling...
go1.16 successfully installed!

Hi! I already encountered an error like that one. I’ve had to add CGO_CPPFLAGS=-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wbuiltin-requires-header to my env to make it work. I think that it’s just removing the compilation flags. My program is the same than yours and I was able to make it work using this. I’m using MacOs Catalina 10.15.4

This works for me except -Wbuiltin-requires-header -> -Wno-builtin-requires-header. Then, what add to env is: CGO_CPPFLAGS=-Wno-error -Wno-nullability-completeness -Wno-expansion-to-defined -Wno-builtin-requires-header

where to add and how?

found out… I recently symlinked gcc headers into usr/local/include to get some other project running. After removing these symlinks from usr/local/include, everything works fine again