gomobile init fails to find stdlib.h

1.5k Views Asked by At

I'm trying to make a cross-platform native library using go and found this tutorial for gomobile. However, when I try to initialize it (even if I give the Android NDK path although I don't in the example below) it seems to fail because it can't find stdlib.h.

I tried to do some investigation around cgo but nothing jumped out at me, was hoping someone more familiar with the ecosystem could point me in the right direction.

$ gomobile init
gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/myusername/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

My environment... (I'm on OS X El Capitan with fresh install of go1.9 darwin/amd64)

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/myusername/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/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/rx/l7pzlrld2mqczrfb36t536hh0000gp/T/go-build735124167=/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"

EDIT: (Adding verbose output of failed init)

$ gomobile init -v

# Installing golang.org/x/mobile/gl.

# Installing golang.org/x/mobile/app.

# Installing golang.org/x/mobile/exp/app/debug.

# Installing std for android/arm.
runtime/internal/sys
runtime/internal/atomic
runtime
sync/atomic
unicode
internal/race
errors
unicode/utf8
math
math/bits
container/list
container/ring
crypto/subtle
crypto/internal/cipherhw
internal/nettrace
runtime/cgo
vendor/golang_org/x/crypto/poly1305
sync
encoding
unicode/utf16
image/color
internal/cpu
internal/syscall/windows
internal/syscall/windows/registry
image/color/palette
internal/syscall/windows/sysdll
io
syscall
internal/singleflight
runtime/race
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found
vendor/golang_org/x/text/secure
vendor/golang_org/x/text/unicode
bytes
strings
hash
crypto/cipher
runtime/trace
hash/crc32
hash/adler32
crypto/hmac
hash/crc64
hash/fnv
vendor/golang_org/x/text/transform
text/tabwriter
bufio
path
html
strconv
math/rand
math/cmplx
time
internal/syscall/unix
reflect
crypto/aes
crypto
crypto/rc4
encoding/base64
encoding/base32
encoding/ascii85
crypto/sha512
crypto/md5
crypto/sha1
crypto/sha256
image
internal/poll
image/internal/imageutil
image/draw
image/jpeg
os
os/signal
fmt
sort
encoding/binary
path/filepath
compress/bzip2
encoding/pem
container/heap
regexp/syntax
runtime/debug
crypto/des
vendor/golang_org/x/crypto/chacha20poly1305/internal/chacha20
vendor/golang_org/x/crypto/curve25519
vendor/golang_org/x/crypto/chacha20poly1305
io/ioutil
compress/flate
context
archive/tar
math/big
compress/lzw
encoding/hex
debug/dwarf
debug/gosym
debug/plan9obj
database/sql/driver
encoding/csv
encoding/gob
encoding/json
database/sql
encoding/xml
archive/zip
compress/gzip
compress/zlib
debug/macho
debug/elf
debug/pe
log
vendor/golang_org/x/net/http2/hpack
vendor/golang_org/x/text/unicode/bidi
vendor/golang_org/x/text/unicode/norm
net/url
mime
mime/quotedprintable
net/http/internal
flag
crypto/dsa
crypto/elliptic
encoding/asn1
crypto/rand
go/token
go/scanner
crypto/rsa
vendor/golang_org/x/text/secure/bidirule
regexp
text/template/parse
go/ast
go/constant
os/exec
text/scanner
crypto/ecdsa
crypto/x509/pkix
image/gif
image/png
vendor/golang_org/x/net/idna
index/suffixarray
testing
internal/trace
runtime/pprof
text/template
net/internal/socktest
os/user
runtime/pprof/internal/profile
testing/iotest
testing/quick
go/parser
go/printer
testing/internal/testdeps
internal/testenv
go/doc
html/template
go/types
go/format
go/build
go/internal/gccgoimporter
go/internal/gcimporter
go/internal/srcimporter
go/importer
gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/myusername/go/pkg/gomobile/pkg_android_arm -v std failed: exit status 2
1

There are 1 best solutions below

0
On

The issue was still reproduced on go1.10 darwin/amd64 even with issue21802 patched.

If go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/myusername/go/pkg/gomobile/pkg_android_arm std can be run on its own it succeeds, assuming the problem is an Runtime ENV problem, there is a workaround.

The trick to solve this problem:

1.Run go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/myusername/go/pkg/gomobile/pkg_android_arm std manually. Make sure it is installed successfully.

2.Then silent installStd inside init.go.

go/src/golang.org/x/mobile/cmd/gomobile/init.go (around Line 165-175)
// Install standard libraries for cross compilers.
start := time.Now()
//if ndkRoot != "" {
    // Ideally this would be -buildmode=c-shared.
    // https://golang.org/issue/13234.
    // androidArgs := []string{"-gcflags=-shared", "-ldflags=-shared"}
    // for _, arch := range archs {
        // env := androidEnv[arch]
        // if err := installStd(env, androidArgs...); err != nil {
            // return err
        // }
    // }
//}

3.Install the workaround gomobile again: go install golang.org/x/mobile/cmd/gomobile

4.Run gomobile init again.