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/go: exported functions in non-main package do not generate header files #68725

Closed
cylixlee opened this issue Aug 4, 2024 · 2 comments
Closed

Comments

@cylixlee
Copy link

cylixlee commented Aug 4, 2024

Go version

go version go1.22.5 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Cylix\AppData\Local\go-build
set GOENV=C:\Users\Cylix\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=G:\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=G:\Go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Cylix\AppData\Local\Temp\go-build919616965=/tmp/go-build -gno-record-gcc-switches

What did you do?

I want to build my package as native libraries available across PC (windows, linux, macos) and mobile (android, ios) platforms, so I'm exploring a way to take advantage of go build -buildmode=c-shared and gomobild bind at the same time.

It's clear that there's one difference between the two tools: go build -buildmode=c-shared requires a main package, and gomobile bind does not allow binding main package. Naturally, I've created a demo project layout as below:
image

The pkg package contains C implementation of a simple function NativeHello, and the corresponding Go code simply calling it.
image

It's now gomobile bind-able. To match go -buildmode=c-shared's requirement, the main.go contains an empty main function and two imports: one for cgo, and one for pkg:
image

What did you see happen?

So I run the two commands separately, and I see the expected result on the gomobile side. Exceptionally, the go build does generate a DLL (I'm on Windows) but no header files (*.h) is generated. I checked the DLL file and see the ManagedHello is exported. It's just no header files.
image

What did you expect to see?

I'm expecting a header file containing the definition of the exported funtions (in my case, the ManagedHello function). I guess it's because I did'nt write export functions in main.go, or more specifically, the main package?

P.S. If I write a wrapper function with the same name (ManagedHello) in main package, which just simply calls pkg.ManagedHello(), the go compiler will complain duplicate definition.

@seankhliao
Copy link
Member

Duplicate of #35715

@seankhliao seankhliao marked this as a duplicate of #35715 Aug 4, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants