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/cgo: a misleading/puzzling error message #70472

Closed
davecb opened this issue Nov 20, 2024 · 4 comments
Closed

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

davecb opened this issue Nov 20, 2024 · 4 comments
Labels
BadErrorMessage Issues related compiler error messages that should be better. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@davecb
Copy link

davecb commented Nov 20, 2024

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

@dmitshur dmitshur changed the title A misleading/puzzling error message from CGO cmd/cgo: a misleading/puzzling error message Nov 20, 2024
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 20, 2024
@dmitshur dmitshur added this to the Backlog milestone Nov 20, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 20, 2024
@rsc
Copy link
Contributor

rsc commented Nov 20, 2024

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
Copy link
Member

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

@dmitshur dmitshur added BadErrorMessage Issues related compiler error messages that should be better. NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 20, 2024
@gopherbot
Copy link
Contributor

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

@github-project-automation github-project-automation bot moved this from Todo to Done in Go Compiler / Runtime Nov 20, 2024
@dmitshur dmitshur modified the milestones: Backlog, Go1.24 Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BadErrorMessage Issues related compiler error messages that should be better. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

6 participants