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: exporting functions breaks if CGO_FLAGS enables LTO #45275

Closed
mwhudson opened this issue Mar 28, 2021 · 3 comments
Closed

cmd/cgo: exporting functions breaks if CGO_FLAGS enables LTO #45275

mwhudson opened this issue Mar 28, 2021 · 3 comments

Comments

@mwhudson
Copy link
Contributor

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

$ go version
go version devel +23ffb5b9ae Sun Mar 28 03:27:04 2021 +0000 linux/amd64

(but also everything else I've tried)

Does this issue reproduce with the latest release?

Yes.

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

Ubuntu 20.04 amd64 -- but fine details are not important.

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mwhudson/.cache/go-build"
GOENV="/home/mwhudson/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mwhudson/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mwhudson/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/7221"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/7221/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mwhudson/src/go/src/go.mod"
CGO_CFLAGS="-flto"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2567708190=/tmp/go-build -gno-record-gcc-switches"

What did you do?

(master)mwhudson@anduril:~/src/go/src$ cat ~/src/go-test-cases/cgocallback.go
package main

//
import "C"

//export MyFunction
func MyFunction(arg1, arg2 int) int {
	return arg1 + arg2
}

func main() {

}
(master)mwhudson@anduril:~/src/go/src$ CGO_CFLAGS=-flto ~/src/go/bin/go build ~/src/go-test-cases/cgocallback.go

What did you expect to see?

No output and a binary built.

What did you see instead?

# command-line-arguments
_cgo_export.c:21:13: error: variable ‘_cgoexp_604c78453144_MyFunction’ redeclared as function
cgo-generated-wrappers:1:5: note: previously declared here
lto1: fatal error: errors during merging of translation units
compilation terminated.
lto-wrapper: fatal error: /usr/bin/gcc returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
@gopherbot
Copy link

Change https://golang.org/cl/305290 mentions this issue: cmd/cgo: make dummy definition of wrapper for exported function match declaration

@ianlancetaylor
Copy link
Contributor

Dup of #43830? Does https://golang.org/cl/293290 fix the problem?

@mwhudson
Copy link
Contributor Author

Yes, and that fixes my problem and one I'd missed and has tests so I'd say definitely prefer that one over mine :)

@golang golang locked and limited conversation to collaborators Mar 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants