I am trying to build the standard "Hello world!" Android command line executable The executable file must be run through the adb shell .
0. Source Go (Golang)
package main import ( "fmt" ) func main() { fmt.Println("Hello, world!") }
1A. Build Command
$ CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build .
1B. Exit (line breaks rearranged to prevent scrollbars)
# github.com/asukakenji/cross warning: unable to find runtime/cgo.a /usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1 ld: warning: ignoring file /var/folders/dd/6k6vkzbd6d5803xj9zkjdhmh0000gn/T/go-link-150305609/go.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/dd/6k6vkzbd6d5803xj9zkjdhmh0000gn/T/go-link-150305609/go.o Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
1C. Build Team, Again
The following command gives the same result:
$ env CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build .
2. Build team (verbose)
I tried using "-v" as mentioned this way:
$ CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build \ -x -ldflags "-extldflags -v" .
It gives me more than 100 lines of messages, so I do not post it here unless necessary. The go build seems to be trying to compile the source code using the clang bundled with Xcode .
3A. Build Command (Successful, but ...)
Given the hint that the wrong compiler was found, I tried to install $CC as follows:
$ CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 \ CC=/path/to/arm-linux-androideabi/bin/clang go build .
arm-linux-androideabi is the output of make_standalone_toolchain.py (or make-standalone-toolchain.sh ).
3B. Output
The executable (named cross ) was successfully compiled with the following messages:
# github.com/asukakenji/cross warning: unable to find runtime/cgo.a
I tried adb push it and ran it from adb shell on Android, everything worked fine.
My questions
- Why does he need a C compiler? Is cross compilation out of the box?
When building for Linux (instead of Android), compilation works fine:
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .
What for?
- The
go build continues to search for runtime/cgo.a even when I did not use CGO in the source code and even when I set CGO_ENABLED=0 . How can I get rid of the warning? How harmful is it without having one?