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:
The pkg
package contains C implementation of a simple function NativeHello
, and the corresponding Go code simply calling it.
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
:
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.
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.
Comment From: gabyhelp
Related Issues and Documentation
- cmd/cgo: generated header for buildmode=c-archive lacks exported symbols for imported library #27150 (closed)
- doc: for -buildmode=c-shared, document that //export requires import "C" #11955 (closed)
- cmd/go: -buildmode=c-shared without specifying output file fails does not add .dll extension on Windows and .so extension on linux #38244
- affected/package: go command #58672 (closed)
- cmd/link: cross compile from MacOS to Windows with CGO_ENABLED=1 and -buildmode=c-archive not working #59221 (closed)
- cmd/go: missing entry in c-shared .dll #42409 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: seankhliao
Duplicate of #35715