Golang:在Mac上为Android编译“Hello World”
我正在努力建立一个标准的“Hello,World!”
Android的命令行可执行文件.可执行文件将通过adb shell运行.
0. Go(Golang)来源 package main import ( "fmt" ) func main() { fmt.Println("Hello,world!") } 1A.构建命令 $CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build . 1B.输出(换行重新排列以防止滚动条) # 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.再一次构建命令 以下命令给出相同的结果: $env CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build . 2.构建命令(详细) 我尝试使用“-v”如下所述: $CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build -x -ldflags "-extldflags -v" . 它给了我超过100行的消息,所以除非有必要,否则我不会在此发布. go build命令似乎尝试使用与Xcode捆绑的clang编译源代码. 3A.构建命令(成功,但……) 鉴于提示找到了错误的编译器,我试图像这样设置$CC: $CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 CC=/path/to/arm-linux-androideabi/bin/clang go build . arm-linux-androideabi是make_standalone_toolchain.py(或make-standalone-toolchain.sh)的输出. 3B.输出 成功构建了可执行文件(名为cross),并显示以下消息: # github.com/asukakenji/cross warning: unable to find runtime/cgo.a 我试过adb push it并在Android上使用adb shell运行它,它运行正常. 我的问题 >为什么需要C编译器?是不是开箱即用的交叉编译? $CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build . 为什么? 谢谢! 解决方法
Android不是交叉编译的官方目标平台.如果你需要的只是命令行可执行文件,那么你可以设置GOOS = linux因为android是linux下的linux,否则看看
https://github.com/golang/go/wiki/Mobile
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |