go: x/mobile: gomobile bind does not work with cgo and android

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

go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/anjmao/Library/Caches/go-build"
GOENV="/Users/anjmao/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/anjmao/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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=/var/folders/p8/5ggx21z124n8kxx2wfv674380000gp/T/go-build165303278=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have this simple test package with cgo and I’m building it for android with android-ndk-r20.

package test

// #include <stdlib.h>
import "C"

type cCharPointer struct {
	Ptr *C.char
}

func Hello() string {
	return "hi"
}

Build:

gomobile bind -o App.aar -target=android $GOPATH/mobile/test

What did you expect to see?

No error. Note that:

  1. It works if I specify ios target.
  2. It works for both android and ios if cgo is not used.
  3. It works with cgo for older gomobile version. Tested with this commit

What did you see instead?

Error:

gomobile: /Users/anjmao/go/bin/gobind -lang=go,java -outdir=/var/folders/p8/5ggx21z124n8kxx2wfv674380000gp/T/gomobile-work-476890504 github.com/mobile/test failed: exit status 1
[/Users/anjmao/go/src/github.com/mobile/test/test.go:4:8: could not import C (no metadata for C)]

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Don’t forget to set CGO_ENABLED=1

I set CGO_ENABLED=1 and still get this error. use this command on mac: GOOS=android GOARCH=arm CGO_ENABLED=1 gomobile bind -target=android . when remove cgo’s part , it works ok

My build environment has encountered the same problem, and trying to change the any version of Go can’t be solved.