golang / go

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

cmd/cgo: a misleading/puzzling error message #70472

Closed davecb closed 1 day ago

davecb commented 1 day ago

Go version

go version go1.23.1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/davecb/.cache/go-build'
GOENV='/home/davecb/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/davecb/go/pkg/mod'
GONOPROXY='*.indexexchange.com'
GONOSUMDB='*.indexexchange.com'
GOOS='linux'
GOPATH='/home/davecb/go'
GOPRIVATE='*.indexexchange.com'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go1.23'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go1.23/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/davecb/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3678357838=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I made a spelling mistake, and tried to link to a CGO function that didn't exist, "C.Cstring". To my surprise I got ./minimum_code.go:15:13: could not determine kind of name for C.Cstring That has me looking in all the wrong places!

When, after a nice cup of tea, I spotted the typo, and got a far better error, /tmp/go-build/minimum_code.cgo2.c:51:(.text+0x8): undefined reference to PDQ_Init

I had been thinking that calling the misspelled function would get me an undefined-reference error, which is more normal.

If this is something that can be fixed, I'd recommend it: Mr Google found lots and lots of discussion of could not determine kind of name for C.Cstring, none relevant. Even a better error message would suffice.

If there really is a Cstring function that you want to keep, bug me about controlling its visibility: Paul Stachour and I have written in the Communications of the ACM about that problem (;-))

package main

// #include <stdio.h>
// #include <math.h>
// #include "../pdq5/lib/PDQ_Lib.h"
import "C"

func main() {
    TestInit()
}

// TestInit is the startup function for the pdq library, tested here
func TestInit() {
    var title string = "closed uniserver"
    C.PDQ_Init(C.Cstring(title)) // title for the report
}

// Results: with C.Cstring, which is spelled wrong, I got
// ./minimum_code.go:15:13: could not determine kind of name for C.Cstring
// with C.CString, I got a good, expected error from ld, which couldn't find PDQ_Init()
// /tmp/go-build/minimum_code.cgo2.c:51:(.text+0x8): undefined reference to `PDQ_Init'
// I'm surprised I didn't get a linkage error for C.Cstring()

What did you see happen?

When misspelled, I got ./minimum_code.go:15:13: could not determine kind of name for C.Cstring

when fixed, I got /tmp/go-build/minimum_code.cgo2.c:51:(.text+0x8): undefined reference to `PDQ_Init'

I'm surprised I didn't get a linkage error for a missing C.Cstring()

What did you expect to see?

./minimum_code.go:15:13: undefined reference to Cstring

gabyhelp commented 1 day ago

Related Issues

Related Discussions

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

rsc commented 1 day ago

If you write a C program (with a modern compiler at least) and call an undeclared function, you don't get to the linker. You get an error about the function name not being known. That's what is happening here. I understand being confused when you wrote C.Cstring and thought you'd written C.CString, but the error is accurate: maybe there really is a library providing Cstring, and if so cgo can't find it. The same error happens if you do C.asdf or any other misspelling of any function name. I'm not sure what we can change here.

ianlancetaylor commented 1 day ago

I think we can be clearer than "could not determine kind of name". I'll send a CL.

gopherbot commented 1 day ago

Change https://go.dev/cl/630375 mentions this issue: cmd/cgo: improve error message for unknown name