Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: 'go build' encountered invalid C headers under MacOS #30072

Closed
hbprotoss opened this issue Feb 4, 2019 · 11 comments
Closed

cmd/go: 'go build' encountered invalid C headers under MacOS #30072

hbprotoss opened this issue Feb 4, 2019 · 11 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@hbprotoss
Copy link

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

$ go version
go version go1.11.5 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hbprotoss/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/hbprotoss/.go:/Users/hbprotoss/Documents/dev/Projects/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
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/s_/2knwlbxn7dvc7zqdkk8x850r0000gn/T/go-build921630012=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Actually, I'm using Golang to debug go program, after I hit the debug button, it didn't compile.

The command used to compile is

go build -gcflags "all=-N -l" main.go

What did you expect to see?

No error

What did you see instead?

It reports

runtime/cgo

In file included from gcc_libinit.c:8:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/pthread.h:232:66: error: unknown type name 'size_t'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/pthread.h:249:43: error: unknown type name 'size_t'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/pthread.h:256:66: error: unknown type name 'size_t'
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/pthread.h:524:1: error: unknown type name 'size_t'
gcc_libinit.c:97:18: error: variable has incomplete type 'struct timespec'
gcc_libinit.c:97:9: note: forward declaration of 'struct timespec'
gcc_libinit.c:105:14: error: use of undeclared identifier 'EAGAIN'
gcc_libinit.c:110:3: error: implicit declaration of function 'nanosleep' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
gcc_libinit.c:112:9: error: use of undeclared identifier 'EAGAIN'

@bcmills
Copy link
Contributor

bcmills commented Feb 4, 2019

These sorts of symptoms on macOS often indicate a broken Xcode install or some other, incompatible gcc toolchain earlier in $PATH.

In this particular case, it appears that you have an invalid pthread.h: size_t is defined in stddef.h, which pthread.h should #include as needed. Similarly, struct timespec should be provided by time.h (#included here); the fact that it is missing suggests that something is very wrong in your C include path.

@bcmills
Copy link
Contributor

bcmills commented Feb 4, 2019

Are you able to build C programs in the same environment?

You may be able to run which clang and/or find / -name time.h to locate the bad headers; as a last resort, you could try removing and reinstalling Xcode.

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 4, 2019
@bcmills bcmills changed the title go build failed with multiple unknown type under MacOS cmd/go: 'go build' encountered invalid C headers under MacOS Feb 4, 2019
@bcmills bcmills added OS-Darwin NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 4, 2019
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@drwasho
Copy link

drwasho commented Jun 21, 2019

FYI if anyone like me found themselves in this situation, check your /usr/local/include folder for header files *.h.

Delete these and you'll get unstuck. Don't ask me how I got myself into that situation, but I think it was screwing around with brew.

@zhulinwei
Copy link

zhulinwei commented Aug 9, 2019

Are you able to build C programs in the same environment?

You may be able to run which clang and/or find / -name time.h to locate the bad headers; as a last resort, you could try removing and reinstalling Xcode.

@bcmills Please help me, I meet this problem too.

I can build C programs in the project, but when I run go test . command, then terimnal will show the message of gcc_libinit.c: 105:14: error: use of undeclared identifier 'EAGAIN😭

@ianlancetaylor
Copy link
Contributor

@zhulinwei This issue is closed, and at this point there is no reason to think that there is a problem with Go. You will most likely find a better answer on a forum. See https://golang.org/wiki/Questions.

@zhulinwei
Copy link

@ianlancetaylor thanks very much, I fix this problem by env CGO_ENABLED=0 go test, but I am not sure if it will cause other problems.

@gautamp8
Copy link

FYI if anyone like me found themselves in this situation, check your /usr/local/include folder for header files *.h.

Delete these and you'll get unstuck. Don't ask me how I got myself into that situation, but I think it was screwing around with brew.

Same. This worked for me. Was afraid of removing to bulk renamed them to .x from .h

@zhulinwei
Copy link

I solved this problem, and I hope my answer can help others.

If you don't need to work with c lang, you can export CGO_ENABLED=0,

else you can change your CC, replace clang with gcc, eg export CC=gcc

@abanobmikaeel
Copy link

abanobmikaeel commented Apr 23, 2020

I know this is older but this solved my problem
go env -w CGO_ENABLED=0
Unclear whether or not it'll cause other problems though.

@bcmills
Copy link
Contributor

bcmills commented Apr 23, 2020

For what it's worth, we've seen a lot of macOS C issues from folks who have installed Anaconda or Miniconda (see #29597). It might be worth investigating those as the source of corrupted C headers.

@golang golang locked and limited conversation to collaborators Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

8 participants