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 init fails with new NDK 16.0rc #21802

Closed
zllak opened this issue Sep 8, 2017 · 14 comments

Comments

Projects
None yet
8 participants
@zllak
Copy link

commented Sep 8, 2017

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

go version go1.9 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/zllak/work/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v3/g9vzz14155s1ffjhjng6zsj00000gn/T/go-build198615708=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

With the latest NDK 16.0rc, gomobile init fails:

gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/zllak/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found

This is very likely due to the Unified Headers migration (https://android.googlesource.com/platform/ndk/+/ndk-release-r16/docs/UnifiedHeadersMigration.md).
This migration removes the headers from $ANDROID_HOME/ndk-bundle/platforms/android-XX/arch-XXX/usr/include

What did you expect to see?

A successful run of gomobile init

What did you see instead?

gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/zllak/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found

@gopherbot gopherbot added this to the Unreleased milestone Sep 8, 2017

@gopherbot gopherbot added the mobile label Sep 8, 2017

@steeve

This comment has been minimized.

Copy link
Contributor

commented Sep 17, 2017

After reading the documentation for unified headers, I got it working with the following diff. You'll need to make sure to rm -rf $GOPATH/pkg/gomobile to clear out the build cache, too.

diff --git a/cmd/gomobile/env.go b/cmd/gomobile/env.go
index bda72ec..9445f95 100644
--- a/cmd/gomobile/env.go
+++ b/cmd/gomobile/env.go
@@ -124,11 +124,14 @@ func envInit() (err error) {
                                a = "armv7a"
                        }
                        target := strings.Join([]string{a, "none", os, env}, "-")
-                       sysroot := filepath.Join(ndkRoot, "platforms", toolchain.platform, "arch-"+toolchain.arch)
+                       androidApi := strings.TrimPrefix(toolchain.platform, "android-")
+                       sysroot := filepath.Join(ndkRoot, "sysroot")
+                       isystem := filepath.Join(sysroot, "usr", "include", toolchain.toolPrefix)
+                       ldsysroot := filepath.Join(ndkRoot, "platforms", toolchain.platform, "arch-"+toolchain.arch)
                        gcctoolchain := filepath.Join(ndkRoot, "toolchains", toolchain.gcc, "prebuilt", archNDK())
-                       flags := fmt.Sprintf("-target %s --sysroot %s -gcc-toolchain %s", target, sysroot, gcctoolchain)
-                       cflags := fmt.Sprintf("%s -I%s/include", flags, gomobilepath)
-                       ldflags := fmt.Sprintf("%s -L%s/usr/lib -L%s/lib/%s", flags, sysroot, gomobilepath, arch)
+                       flags := fmt.Sprintf("-target %s -gcc-toolchain %s", target, gcctoolchain)
+                       cflags := fmt.Sprintf("%s --sysroot %s -isystem %s -D__ANDROID_API__=%s -I%s/include", flags, sysroot, isystem, androidApi, gomobilepath)
+                       ldflags := fmt.Sprintf("%s --sysroot %s -L%s/lib/%s", flags, ldsysroot, gomobilepath, arch)
                        androidEnv[arch] = []string{
                                "GOOS=android",
                                "GOARCH=" + arch,
diff --git a/internal/mobileinit/mobileinit_android.go b/internal/mobileinit/mobileinit_android.go
index 049b18e..cd73f43 100644
--- a/internal/mobileinit/mobileinit_android.go
+++ b/internal/mobileinit/mobileinit_android.go
@@ -19,6 +19,7 @@ adb logcat GoLog:I *:S
 #cgo LDFLAGS: -landroid -llog

 #include <android/log.h>
+#include <stdlib.h>
 #include <string.h>
 */
 import "C"
@TMSConsulting

This comment has been minimized.

Copy link

commented Sep 25, 2017

I was not able to get a successful run. Same error as @zllak. Did anyone else edit these two files and then get gomobile init to work?

@steeve

This comment has been minimized.

Copy link
Contributor

commented Sep 25, 2017

@TMSConsulting I work with @zllak, you'll need to clear your GOPATH/pkg/gomobile path too

@TMSConsulting

This comment has been minimized.

Copy link

commented Sep 25, 2017

@steeve, thank you for your note. I got it work. Long live github!

@steeve

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2017

@eliasnaur how can we go about merging this one? not sure how to contribute

@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2017

@steeve, have you signed the CLA (https://golang.org/doc/contribute.html#cla)? If so, I'll take care of merging the patch for you.

@steeve

This comment has been minimized.

Copy link
Contributor

commented Oct 11, 2017

Signed!

@gopherbot

This comment has been minimized.

Copy link

commented Oct 11, 2017

Change https://golang.org/cl/69950 mentions this issue: cmd/gomobile, internal/mobileinit: update to the NDK unified headers

@txthinking

This comment has been minimized.

Copy link

commented Oct 24, 2017

Is this released?
For now, the bug still here after go get -u golang.org/x/mobile/cmd/gomobile

@steeve

This comment has been minimized.

Copy link
Contributor

commented Oct 24, 2017

@txthinking it is released, yes

@txthinking

This comment has been minimized.

Copy link

commented Oct 24, 2017

Thanks. fixed.

@cxy200927099

This comment has been minimized.

Copy link

commented Nov 14, 2017

@steeve i have still get this error! could you tell me how to solve this?
I first install gomobile:

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

It was always failed, This is because i live in china,The website golang.org can not be opened;
Then i use following method:

//clone it to $GOPATH/srcgolang.org/x/mobile
git clone https://github.com/golang/mobile.git $GOPATH/src/golang.org/x/mobile
//build it byself and install
cd $GOPATH
go build golang.org/x/mobile/cmd/gomobile
go install golang.org/x/mobile/cmd/gomobile

After this i install gomobile sucessfull

But when i exute

gomobile init 

I get the error:

➜  go gomobile init              
gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/chenxingyi/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found

And you said the problem was sloved in the release version, but i still get the error! my go env as bellow:

➜  go go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/chenxingyi/work/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nk/xlblxlxx7g13b29sql7c7_hc0000gn/T/go-build420391291=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
@cxy200927099

This comment has been minimized.

Copy link

commented Nov 14, 2017

@steeve I have solve the problem by using android studio update ndk version(15.2.4203891)

image

Then clear build cache and excute gomobile init again,It works

rm -rf $GOPATH/pkg/gomobile
gomobile init

camlistorebot pushed a commit to perkeep/perkeep that referenced this issue Nov 20, 2017

clients/android: update gomobile to fix docker build
Due to a change in the Android NDK (starting from r16), building the
docker container fails with an error during the installation of
gomobile:
    fatal error: 'stdlib.h' file not found

This issue was addressed in more recent versions of gomobile, which
are compatible with the new NDK.
See golang/go#21802

This CL simply updates gomobile to the most recent commit.

Change-Id: I87c468c34de441bc6acd40b717d1d950014e5a99
@royfalk

This comment has been minimized.

Copy link

commented Nov 21, 2017

rm -rf $GOPATH/pkg/gomobile did not work for me with NDK 16.0.4442984

Updating did the trick.

mpl added a commit to mpl/go4droid that referenced this issue Dec 21, 2017

Dockerfile: update gomobile
Due to a change in the Android NDK (starting from r16), building the
docker container fails with an error during the installation of
gomobile:
    fatal error: 'stdlib.h' file not found

This issue was addressed in more recent versions of gomobile, which
are compatible with the new NDK.
See golang/go#21802

@golang golang locked and limited conversation to collaborators Nov 21, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.