golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.98k stars 17.53k forks source link

cmd/cgo: building c-archive with function pointer works with Go 1.14, but not 1.15 and beyond #44648

Open willie opened 3 years ago

willie commented 3 years ago

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

$ go version
go version go1.15 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/willie/Library/Caches/go-build"
GOENV="/Users/willie/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/willie/pkg/mod"
GONOPROXY="github.com/vitalsource/*"
GONOSUMDB="github.com/vitalsource/*"
GOOS="darwin"
GOPATH="/Users/willie"
GOPRIVATE="github.com/vitalsource/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/willie/.asdf/installs/golang/1.15/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/willie/.asdf/installs/golang/1.15/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/willie/willie/cgotest/go.mod"
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/4z/2t7q40b14znb__08yd_p_znw0000gn/T/go-build009670235=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I build a Go c-archive static library to use in several C/C++ programs. Building c-archives with cgo broke in a way that I don't understand in Go 1.15 and later. Building with Go 1.14 works fine. I put together a simple example here: https://github.com/willie/cgotest

What did you expect to see?

I expected to be able to build a c-archive static library with no errors.

What did you see instead?

# command-line-arguments
_cgo_export.c:29:3: error: unknown type name 'writeFunc'
ianlancetaylor commented 3 years ago

The problem here is

func HTTPGet(url *C.char, writeFunc C.writeFunc, writeFuncData unsafe.Pointer) (read int64) {

Note that writeFunc is both the Go parameter name and the C type name. This leads to confusion in the generated C code.

willie commented 3 years ago

YES! Changing the Go parameter name fixed the problem. Such a relief and an easy change. Thank you for the help @ianlancetaylor

dmitshur commented 3 years ago

@ianlancetaylor Ian, is there something to do here for Go 1.17, or is it appropriate to move it to Backlog until it becomes more clear what the timeline of a fix might be?

ianlancetaylor commented 3 years ago

Moving to Backlog.