golang交叉編譯

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

本地開發環境是os x 伺服器是linux,所以需要編譯linux用的go程式,方法如下:
各平台的GOOS和GOARCH參考

OS ARCH OS versionv

linux 386 / amd64 / arm >= Linux 2.6

darwin 386 / amd64 OS X (Snow Leopard + Lion)

freebsd 386 / amd64 >= FreeBSD 7

windows 386 / amd64 >= Windows 2000

$ cd /usr/local/go/src$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

這裡並不是重新編譯Go,因為安裝Go的時候,只是編譯了本地系統需要的東西;而需要跨平台交叉編譯,需要在Go中增加對其他平台的支援。所以,有 ./make.bash 這麼一個過程。

執行結果類似如下:

sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bashPassword:Building C bootstrap tool.cmd/distBuilding compilers and Go bootstrap tool for host, darwin/amd64.lib9libbiolibmachmisc/pprofcmd/addr2linecmd/covcmd/nmcmd/objdumpcmd/packcmd/profcmd/cc……pkg/text/template/parsepkg/text/templatepkg/go/docpkg/go/buildcmd/gopkg/runtime (linux/amd64)Building packages and commands for host, darwin/amd64.runtimeerrorssync/atomicunicodeunicode/utf8mathsyncunicode/utf16crypto/subtleiosyscall……….net/rpc/jsonrpctesting/iotesttesting/quickBuilding packages and commands for linux/amd64.runtimeerrorssync/atomicunicodeunicode/utf8mathsyncunicode/utf16……..testingnet/rpc/jsonrpctesting/iotesttesting/quickInstalled Go for linux/amd64 in /usr/local/goInstalled commands in /usr/local/go/bin

到原始碼目錄下執行:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build sina.go

不帶前面參數的 go build 只是編譯出開發環境適用的執行檔案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.