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

Comments

@anjmao
Copy link

@anjmao 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 label Nov 7, 2019
@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Nov 8, 2019

@anjmao

This comment has been minimized.

Copy link
Author

@anjmao 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

This comment has been minimized.

Copy link
Author

@anjmao 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 Nov 8, 2019
@anjmao

This comment has been minimized.

Copy link
Author

@anjmao 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

This comment has been minimized.

Copy link

@zongquan zongquan commented Nov 20, 2019

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

@JasonHonor

This comment has been minimized.

Copy link

@JasonHonor JasonHonor commented Nov 27, 2019

Me too, anyone can help me ?

@bbadbeef

This comment has been minimized.

Copy link

@bbadbeef bbadbeef commented Dec 18, 2019

Me too, anyone can help me ?

me too. have you solve it yet?

@steeve

This comment has been minimized.

Copy link
Contributor

@steeve steeve commented Dec 18, 2019

Don't forget to set CGO_ENABLED=1

@bbadbeef

This comment has been minimized.

Copy link

@bbadbeef 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

This comment has been minimized.

Copy link

@bbadbeef bbadbeef commented Dec 19, 2019

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

@bestbug456

This comment has been minimized.

Copy link

@bestbug456 bestbug456 commented Dec 19, 2019

@bbadbeef which version did you use?

@bbadbeef

This comment has been minimized.

Copy link

@bbadbeef 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

This comment has been minimized.

Copy link

@cswxzx cswxzx commented Dec 23, 2019

Me too, anyone can help me ?

me too. have you solve it yet?

@bestbug456

This comment has been minimized.

Copy link

@bestbug456 bestbug456 commented Dec 23, 2019

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

This comment has been minimized.

Copy link

@bbadbeef 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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.