搭建golang跨平臺(tái)編譯環(huán)境

環(huán)境配置

  1. 安裝Docker
  2. 安裝golang
  3. 安裝xgo鏡像及編譯指令
  4. 配置環(huán)境變量

1. 安裝docker

針對(duì)Mac和Windows可以直接下載安裝包進(jìn)行安裝,其他系統(tǒng)可以使用命令行進(jìn)行安裝.參考鏈接,最后通過docker version 指令進(jìn)行查看

# Jacen at EveeedeMacBook-Pro.local in ~ [10:51:52] docker version
Client:
Version:      1.12.5
API version:  1.24
Go version:   go1.6.4
Git commit:   7392c3b
Built:        Fri Dec 16 06:14:34 2016
OS/Arch:      darwin/amd64

Server:
Version:      1.12.5
API version:  1.24
Go version:   go1.6.4
Git commit:   7392c3b
Built:        Fri Dec 16 06:14:34 2016
OS/Arch:      linux/amd64

2. 安裝golang

同樣的在Windows和Mac下面都有一鍵安裝包,針對(duì)其他系統(tǒng)也是通過命令行進(jìn)行安裝.參考鏈接,安裝好以后還需要進(jìn)行環(huán)境變量的配置.

在mac系統(tǒng)和Linux系統(tǒng)下面,Windows配置環(huán)境變量

export GOROOT="/usr/local/go"
export GOPATH="你的工作目錄"  //這里主要是影響到后面需要安裝的xgo

再執(zhí)行g(shù)o env查看是否配置成功:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Volumes/Work/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/w4/75xgx41n2bb7mm3m0dxbp0d40000gn/T/go-build003828011=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

3. 安裝xgo docker鏡像

xgo的docker鏡像是我們真正的編譯環(huán)境,Github地址

  1. 安裝xgo docker鏡像,這個(gè)鏡像大概4.xG,需要花較長的時(shí)間下載

    docker pull karalabe/xgo-latest

這里latest指的最新的編譯環(huán)境,如果需要的話也可以指定安裝具體版本的鏡像,在這里,這個(gè)版本的差異主要是golang版本的不同,由于編譯的時(shí)候可以指定golang版本進(jìn)行編譯,所以我們直接安裝最新版本即可.

  1. 安裝xgo

    go get github.com/karalabe/xgo

安裝xgo編譯工具,這個(gè)工具封裝了go build,簡化了我們的操作.

4. 配置環(huán)境變量

如果我們使用的是bash的話可以將環(huán)境變量配置到.bash_profile中,免去export的過程

使用bash
  1. 在 ~ 目錄下編輯.bash_profile 文件,在末尾添加

    export GOPATH="你的工作目錄"
    export GOROOT="usr/local/go"
    export PATH=$PATH:$GOPATH/bin
    export GOROOT_BOOTSTRAP=$GOROOT

  2. source .bash_profile使得配置文件生效

使用如zsh shell程序
  1. 在~ 目錄下編輯.zshrc 文件,添加上面同樣的文件
  2. 同上source生效

5. 測試xgo交叉編譯環(huán)境是否OK

$ cd $GOPATH/src       // If you have multiple (I have at least 3 in there), change into any, doesn't matter
$ mkdir xgo_test
$ cd xgo_test
$ echo -e 'package main\n import "fmt"\n func main(){\nfmt.Println("Hello")\n}' > main.go
$ xgo .

執(zhí)行完上述命令后在當(dāng)前目錄查看是否有編譯生成的可執(zhí)行文件

6. 打包

  ~ xgo  /Volumes/Public\ Storage/go/src/zhjd()
  ~ xgo --targets=android-14/arm /Volumes/Storage/go/src/zhjd 指定生成Android端專用的會(huì)快很多很多,這里的14指的是最小支持SDK版本
Checking docker installation...
Client:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:        Wed Oct 26 23:26:11 2016
 OS/Arch:      darwin/amd64

Server:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:        Wed Oct 26 23:26:11 2016
 OS/Arch:      linux/amd64

Checking for required docker image karalabe/xgo-latest... found.
Cross compiling zhjd...
Building locally zhjd...
Assembling toolchain for android-16/arm...
Bootstrapping android-16/arm...
Compiling for android-16/arm...
Assembling toolchain for android-16/386...
Bootstrapping android-16/386...
Compiling for android-16/386...
No API C header specified, skipping android-16/aar... 重點(diǎn)關(guān)注
Compiling for linux/amd64...
Compiling for linux/386...
Bootstrapping linux/arm-5...
Compiling for linux/arm-5...
Cleaning up Go runtime for linux/arm-5...
Bootstrapping linux/arm-6...
Compiling for linux/arm-6...
Cleaning up Go runtime for linux/arm-6...
Bootstrapping linux/arm-7...
Compiling for linux/arm-7...
Cleaning up Go runtime for linux/arm-7...
Compiling for linux/arm64...
Compiling for linux/mips64...
Compiling for linux/mips64le...
Compiling for windows-4.0/amd64...
Compiling for windows-4.0/386...
Compiling for darwin-10.6/amd64...
Compiling for darwin-10.6/386...
Bootstrapping ios-5.0/arm-7...
# runtime/cgo
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# net
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# os/user
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Compiling for ios-5.0/arm-7...
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Cleaning up Go runtime for ios-5.0/arm-7...
Bootstrapping ios-5.0/arm64...
# runtime/cgo
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# net
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# os/user
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Compiling for ios-5.0/arm64...
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Cleaning up Go runtime for ios-5.0/arm64...
Cleaning up build environment...

查看編譯生成的文件

?  all ls -al
total 427872
drwxr-xr-x  21 silence  staff       714 Dec 29 11:37 .
drwxr-xr-x+ 29 silence  staff       986 Dec 29 11:37 ..
-rw-r--r--@  1 silence  staff      6148 Dec 29 11:37 .DS_Store
-rwxr-xr-x   1 silence  staff  14633608 Dec 29 11:27 zhjd-android-16-386
drwxr-xr-x   5 silence  staff       170 Dec 29 11:27 zhjd-android-16-aar
-rwxr-xr-x   1 silence  staff  14705872 Dec 29 11:26 zhjd-android-16-arm
-rwxr-xr-x   1 silence  staff  10418780 Dec 29 11:32 zhjd-darwin-10.6-386
-rwxr-xr-x   1 silence  staff  11578172 Dec 29 11:31 zhjd-darwin-10.6-amd64
-rwxr-xr-x   1 silence  staff  11224720 Dec 29 11:34 zhjd-ios-5.0-arm64
-rwxr-xr-x   1 silence  staff  10569184 Dec 29 11:33 zhjd-ios-5.0-armv7
drwxrwxrwx   3 silence  staff       102 Dec 29 11:35 zhjd-ios-5.0-framework
-rwxr-xr-x   1 silence  staff  14611432 Dec 29 11:28 zhjd-linux-386
-rwxr-xr-x   1 silence  staff  17246480 Dec 29 11:27 zhjd-linux-amd64
-rwxr-xr-x   1 silence  staff  13034956 Dec 29 11:28 zhjd-linux-arm-5
-rwxr-xr-x   1 silence  staff  12965808 Dec 29 11:29 zhjd-linux-arm-6
-rwxr-xr-x   1 silence  staff  12939788 Dec 29 11:29 zhjd-linux-arm-7
-rwxr-xr-x   1 silence  staff  17075720 Dec 29 11:29 zhjd-linux-arm64
-rwxr-xr-x   1 silence  staff  18700376 Dec 29 11:30 zhjd-linux-mips64
-rwxr-xr-x   1 silence  staff  18699640 Dec 29 11:31 zhjd-linux-mips64le
-rwxr-xr-x   1 silence  staff   9734407 Dec 29 11:31 zhjd-windows-4.0-386.exe
-rwxr-xr-x   1 silence  staff  10889885 Dec 29 11:31 zhjd-windows-4.0-amd64.exe

可以發(fā)現(xiàn),針對(duì)不同平臺(tái)生成了不同類型的文件.Android支持生成aar庫文件,iOS支持生成Framework庫文件

平臺(tái)實(shí)測

Android端測試

  1. 推送到設(shè)備中,之所以不直接推送到app目錄下是因?yàn)闄?quán)限問題,這里如果提示adb命令不存在同樣的也要把Android SDK中的adb命令配置到環(huán)境變量中

     //直接推送到app目錄下會(huì)有權(quán)限問題
     
     adb push zhjd-android-16-arm data/data/com,kingdee.zhihuiji 
     adb: error: failed to copy 'zhjd-android-16-arm' to 'data/data/ com.kingdee.zhihuiji': Permission denied
    
     //所以推送到該目錄下
     
     adb push zhjd-android-16-arm data/local/tmp                
     [100%] data/local/tmp/zhjd-android-16-arm
    
  2. 切換到Android設(shè)備并執(zhí)行該文件,這里只能使用已經(jīng)root的設(shè)備

    adb shell 進(jìn)入手機(jī)命令行
    su 切換超級(jí)用戶
    cd /data/local/tmp 
    ./zhjd-android-16-arm

最后在終端會(huì)輸出可執(zhí)行文件的執(zhí)行結(jié)果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,287評(píng)論 25 708
  • 一、編譯源碼的背景環(huán)境 Android源碼編譯有什么困難 AOSP 非常龐大,需要下載,但是他是Google家的,...
    千山萬水迷了鹿閱讀 13,638評(píng)論 5 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • 2016年7月19日,單位領(lǐng)導(dǎo)突然找我談話,說是下一步要調(diào)整工作崗位,從政工轉(zhuǎn)入辦公室。5年了,很少寫大型材料和...
    飛翔我心閱讀 228評(píng)論 0 0
  • Day 多少了?不記得。一百多天了,還在寫,你真棒! 能打開電腦寫字實(shí)屬回光返照。先是把昨天當(dāng)成了7月的最后一天,...
    不要蔥花兩個(gè)雞蛋閱讀 427評(píng)論 0 0