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

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

Closed
anjmao opened this issue Nov 7, 2019 · 15 comments
Closed

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

anjmao opened this issue Nov 7, 2019 · 15 comments
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Android
Milestone

Comments

@anjmao
Copy link

anjmao commented Nov 7, 2019

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)]
@gopherbot gopherbot added this to the Unreleased milestone Nov 7, 2019
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Nov 7, 2019
@bcmills bcmills added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Android labels Nov 8, 2019
@bcmills
Copy link
Contributor

bcmills commented Nov 8, 2019

CC @steeve @hyangah

@anjmao
Copy link
Author

anjmao commented Nov 8, 2019

Specifically inner gobind command fails to run gobind with these environment variables

GOOS=android CGO_ENABLED=1 gobind -lang=go,java -outdir=./build/bind $GOPATH/mobile/test

I installed go std library for cgo with

TOOLCHAIN=/usr/local/android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64
ARCH=armv7a
export AR=$TOOLCHAIN/bin/$ARCH-linux-android-ar
export AS=$TOOLCHAIN/bin/$ARCH-linux-android-as
export CC=$TOOLCHAIN/bin/$ARCH-linux-androideabi28-clang
export CXX=$TOOLCHAIN/bin/$ARCH-linux-androideabi28-clang++
export LD=$TOOLCHAIN/bin/$ARCH-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/$ARCH-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/$ARCH-linux-android-strip

GOOS=android GOARCH=arm CGO_ENABLED=1 go install std

It would be awesome if some of you @steeve @hyangah could check if gomobile + latest android ndk + clang + cgo works for you.

@anjmao
Copy link
Author

anjmao commented Nov 8, 2019

I found the problem. My command was missing GOARCH env variable. Full command should be

GOOS=android GOARCH=arm CGO_ENABLED=1 gomobile bind ...

@anjmao anjmao closed this as completed Nov 8, 2019
@anjmao
Copy link
Author

anjmao commented Nov 8, 2019

One thing I don't like is that I get this error could not import C (no metadata for C) even If I provide wrong CC env variable, for example setting it to CC=no-such-binary. Maybe it make sense to create separate issue for improvements validating passed env variables when using cgo as it can get really annoying then dealing with these issues.

/CC @bcmills

@zongquan
Copy link

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

@JasonHonor
Copy link

Me too, anyone can help me ?

@bbadbeef
Copy link

Me too, anyone can help me ?

me too. have you solve it yet?

@steeve
Copy link
Contributor

steeve commented Dec 18, 2019

Don't forget to set CGO_ENABLED=1

@bbadbeef
Copy link

bbadbeef commented Dec 19, 2019

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

@bbadbeef
Copy link

i use an older version of gomobile and it works ok with cgo

@bestbug456
Copy link

@bbadbeef which version did you use?

@bbadbeef
Copy link

bbadbeef commented Dec 20, 2019

@bbadbeef which version did you use?

92f3b9c works ok, the newer versions are not

finally i found it's not ok, just not output error message when compiling

@cswxzx
Copy link

cswxzx commented Dec 23, 2019

Me too, anyone can help me ?

me too. have you solve it yet?

@bestbug456
Copy link

Guys for you information for android I've start using this docker images which works fine: openpriv/android-go-mobile at the moment is updated at 10 Jul 2018.

@bbadbeef
Copy link

bbadbeef commented Dec 25, 2019

my go function have c type param and return value, can it be supported with gomobile?
in header files it shows
" // skipped function xxxx with unsupported parameter or return types"

@golang golang locked and limited conversation to collaborators Dec 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Android
Projects
None yet
Development

No branches or pull requests

9 participants