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: undefined reference when using referencing static functions #34831

Closed
abacabadabacaba opened this issue Oct 10, 2019 · 7 comments
Closed
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@abacabadabacaba
Copy link

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

$ go version
go version go1.13.1 linux/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="/path/to/home/.cache/go-build"
GOENV="/path/to/home/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/path/to/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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=/tmp/go-build344992079=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I tried to compile the following program:

package main

// static void f() { }
import "C"

func main() {
	_ = C.f
}

What did you expect to see?

I expected it to compile successfully.

What did you see instead?

# _/path/to/test
/usr/bin/ld: $WORK/b001/_cgo_main.o:/tmp/go-build/cgo-generated-wrappers:2: undefined reference to `f'
collect2: error: ld returned 1 exit status
@ianlancetaylor
Copy link
Contributor

Note that calling C.f works fine.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 10, 2019
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 10, 2019
@shawndx
Copy link
Contributor

shawndx commented Dec 4, 2019

Note that calling C.f works fine.

cgo generates wrapper of a 'C.f' even it's 'static', is it expected behavior of cgo?

@ianlancetaylor
Copy link
Contributor

Yes. But please take discussions of how cgo works to golang-nuts, not this issue. Thanks.

@AdamSLevy
Copy link

How do you work around this?

@ianlancetaylor
Copy link
Contributor

@AdamSLevy Use a Go function literal. For example,

_ = func() { C.f() }

@qmuntal
Copy link
Contributor

qmuntal commented Jan 10, 2021

Possibly related to #19836

@bcmills
Copy link
Contributor

bcmills commented Nov 9, 2021

Duplicate of #19836

@bcmills bcmills marked this as a duplicate of #19836 Nov 9, 2021
@bcmills bcmills closed this as completed Nov 9, 2021
@golang golang locked and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants