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: Getting undefined reference while generating binding using gomobile #31905

Closed
jay11ca39 opened this issue May 8, 2019 · 21 comments
Closed
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.
Milestone

Comments

@jay11ca39
Copy link

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

$ go version: go1.12.4 linux/amd64

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

go env Output
$ go env

GOARCH="amd64"
GOBIN="/usr/local/go"
GOCACHE="/home/--/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="My Go path"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build242944229=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. I have go package, I am trying to generate go binding for that using gomobile tool.
    (a) gomobile bind -o my.aar -target=android mygopackage

  2. Details about my go package [mygopackage]:
    (a) $ tree
    ├── file.go
    └── res.c

    (b) Details about file.go:
    (i) I am including my res.c
    (ii) I am just redirecting my function call to c file.

    (c) Details about my res.c file:
    (i) I am including one header for getting interfaces: #include <ifaddrs.h> :
    http://man7.org/linux/man-pages/man3/getifaddrs.3.html
    and I am calling one API: getifaddrs()

Issue:
While generating the go mobile binding it is giving undefined reference :
$ gomobile bind -o my.aar -target=android mygopackage

gomobile: go build -buildmode=c-shared -o=/tmp/gomobile-work-063445150/android/src/main/jniLibs/armeabi-v7a/libgojni.so gobind failed: exit status 2
'#mygopackage'
src/mygopackage/res.c:15: error: undefined reference to 'getifaddrs'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have browsed all issues but no issue is related to this.

@gopherbot gopherbot added this to the Unreleased milestone May 8, 2019
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label May 8, 2019
@jay11ca39
Copy link
Author

jay11ca39 commented May 8, 2019

Hello @eliasnaur
Can you please help me out in this

@jay11ca39
Copy link
Author

As per my analysis this symbol is present in "libc" library. But I am not sure how gomobile internally links "libc" while we run gomobile tool to generate .aar file.

@eliasnaur
Copy link
Contributor

According to a quick Google search, getifaddrs is not available before Android API level 24. Gomobile builds with a lower API level (21 if I remember correctly). You should be able to work around the issue while maintaining Android compatibility by using dlopen and dlsym to access getifaddrs.

@AlexRouSg
Copy link
Contributor

@eliasnaur is there anything preventing android from having a command line arg to set the API level? I saw IOS has one and find it weird android doesn't.

@eliasnaur
Copy link
Contributor

eliasnaur commented May 8, 2019 via email

@jay11ca39
Copy link
Author

@eliasnaur ,
Is there is any way to specify API level in gomobile bind command ?

@AlexRouSg
Copy link
Contributor

@jay11ca39
Copy link
Author

@eliasnaur ,
I have checked the minimum API level in the mainfest file of one of the generated .aar file using gomobile:
<uses-sdk android:minSdkVersion="15"/></manifest>

Then, it means it is taking minimum API level as 15.

But, Then I tried to include some symbols that is introduced in Android API level: 8
Header file: ethtool.h
Symbol : ethtool_cmd_speed
Link: https://android.googlesource.com/platform/external/kernel-headers/+/froyo/original/linux/ethtool.h

But, it is also giving the undefined reference error:
src/mygopackage/res.c:20: error: undefined reference to 'ethtool_cmd_speed'

As per this testing I got confused that reported issue is related to minimum API level or something else.

Can you please provide your opinion on this.
Thanks in advance.

@eliasnaur
Copy link
Contributor

The uses-sdk declaration is for Java code. A -D__ANDROID_API__=$API is needed somewhere. Perhaps it is enough to use

#cgo CFLAGS -D__ANDROID_API__=24

in your .c file.

@jay11ca39
Copy link
Author

Tried to place in .c file but it did not help.
Tried to explicitly set it using export command in go env from command line, but result is same.

@eliasnaur ,
As I specified in my last comment, even I used the symbol that is there from API level 8 but it given the same error for those symbols as well: ethtool_cmd_speed

I assume minimum API level in gomobile is at least more than 8.

@AlexRouSg
Copy link
Contributor

@jay11ca39 Can you link to docs showing ethtool_cmd_speed is available since API 8? What you linked is the android kernel headers and those are not available to apps.

@jay11ca39
Copy link
Author

@AlexRouSg ,
I thought those will be available to apps. I did not find any doc saying that it is introduced in API 8.

@eliasnaur ,
As currently, gomobile bind command is not taking command line argument for API-Level.
Is there is any way to set manually in gomobile code to set API level and build it..

@AlexRouSg
Copy link
Contributor

@jay11ca39

#31905 (comment)

if you want to implement it, look at https://github.com/golang/mobile/blob/master/cmd/gomobile/env.go#L311 and https://github.com/golang/mobile/blob/master/cmd/gomobile/build.go#L220

"it" referring to the api version command line arg.

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 8, 2019
@jay11ca39
Copy link
Author

#31905 (comment)

@AlexRouSg
Copy link
Contributor

@jay11ca39 what is that last comment suppose to mean? You asked how to get gomobile to use a different API level and I told you where to start looking.

@gopherbot
Copy link

Change https://golang.org/cl/176077 mentions this issue: cmd/gomobile: add arg to set android api level

@jay11ca39
Copy link
Author

Hello @eliasnaur
I have one basic query regarding build gomobile source code:

  1. go get golang.org/x/mobile/cmd/gomobile

  2. Then, i have taken the changes from https://golang.org/cl/176077

  3. Now, I want to rebuild the gomobile.

I did not find steps to build gomobile code from source code.

Can you, let me know how to to build from source ?

@jay11ca39
Copy link
Author

#31905 (comment)
@eliasnaur @AlexRouSg @andybons
Can anybody please provide views on how to build from source? so that I can verify whether given patch solved this issue or not.

@AlexRouSg
Copy link
Contributor

Anything you can obtain with go get is just a normal Go program/package, so you just build it the same way you build any normal Go program/package.

Please see https://golang.org/doc/code.html#remote

But the changes have been merged, this means you can go get -u golang.org/x/mobile/cmd/gomobile to update.

@eliasnaur
Copy link
Contributor

You should be able to just run go install golang.org/x/mobile/cmd/... after applying the change.

@jay11ca39
Copy link
Author

Thanks @eliasnaur , @AlexRouSg for all your help and suggestions.

I am able to build my code using -androidapi command line flag introduced in: https://go-review.googlesource.com/c/mobile/+/176077

@golang golang locked and limited conversation to collaborators May 9, 2020
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 10, 2021
Fixes golang/go#31905

Change-Id: Icee0ece2e78028fa4afd8b273b86e4eed404d99a
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/176077
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 11, 2021
Fixes golang/go#31905

Change-Id: Icee0ece2e78028fa4afd8b273b86e4eed404d99a
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/176077
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
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.
Projects
None yet
Development

No branches or pull requests

5 participants