zoukankan      html  css  js  c++  java
  • Golang的跨平台编译程序

    Golang支持交叉编译,也就是说你在32位平台的机器上开发,可以编译生成64位平台上的可执行程序。

    交叉编译依赖下面几个环境变量:

    $GOARCH    目标平台(编译后的目标平台)的处理器架构(386、amd64、arm)
    $GOOS          目标平台(编译后的目标平台)的操作系统(darwin、freebsd、linux、windows)


    各平台的GOOS和GOARCH参考 

    OS                   ARCH                          OS version

    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


    跨平台编译步骤:

    一、了解目标平台架构:

    下面几个命令有助于了解目标平台的架构:

    [root@localhost ~]# uname -a
    Linux localhost.localdomain 2.6.32-279.19.1.el6.x86_64 #1 SMP Wed Dec 19 07:05:20 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
    [root@localhost ~]# uname -m
    x86_64
    [root@localhost ~]# arch
    x86_64
    [root@localhost ~]# file /bin/cat
    /bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    [root@localhost ~]#

    这里可以看到这个服务器的架构是 x86_64 这个架构亦称 amd64.

    • 6g是amd64的go编译器,它生成的是.6文件。
    • 386一般使用8g命令,它生成的一般是.8格式的文件。
    • 当然还有一个5g的命令是用于arm的cpu,

    同理amd64用6l,386用8l,arm用5l的链接器

    参考资料:

    Linux下如何查看系统是32位还是64位的?
    http://blog.csdn.net/whucs_b701/article/details/8543499

    二、准备目标平台需要的包和工具文件

    执行下面命令:

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

    这里 额外多一个环境变量 CGO_ENABLED 是因为 交叉编译不支持 CGO,我们这里禁用它。

    这里并不是重新编译Go,因为安装Go的时候,只是编译了本地系统需要的东西;而需要跨平台交叉编译,需要在Go中增加对其他平台的支持。所以,有 ./make.bash 这么一个过程。

    执行结果类似如下:

    sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
    Password:
    # Building C bootstrap tool.
    cmd/dist

    # Building compilers and Go bootstrap tool for host, darwin/amd64.
    lib9
    libbio
    libmach
    misc/pprof
    cmd/addr2line
    cmd/cov
    cmd/nm
    cmd/objdump
    cmd/pack
    cmd/prof
    cmd/cc
    …...
    pkg/text/template/parse
    pkg/text/template
    pkg/go/doc
    pkg/go/build
    cmd/go
    pkg/runtime (linux/amd64)

    # Building packages and commands for host, darwin/amd64.
    runtime
    errors
    sync/atomic
    unicode
    unicode/utf8
    math
    sync
    unicode/utf16
    crypto/subtle
    io
    syscall
    ……….
    net/rpc/jsonrpc
    testing/iotest
    testing/quick

    # Building packages and commands for linux/amd64.
    runtime
    errors
    sync/atomic
    unicode
    unicode/utf8
    math
    sync
    unicode/utf16
    ……..
    testing
    net/rpc/jsonrpc
    testing/iotest
    testing/quick


    ---
    Installed Go for linux/amd64 in /usr/local/go
    Installed commands in /usr/local/go/bin

    安装完成后,我们可以看到 Go的安装目录下 多了这个平台特有的几个命令行工具。

    NewImage

    三、编译对应平台下的执行文件

    到源代码目录下执行:

    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

    不带前面参数的 go build 只是编译出开发环境适用的执行文件。

    参考资料:

    Cross compile Go on OSX?
    http://stackoverflow.com/questions/12168873/cross-compile-go-on-osx

    跨平台编译Go程序
    http://bbs.studygolang.com/thread-65-1-1.html
    http://solovyov.net/en/2012/cross-compiling-go/
    http://studygolang.com/topics/21
     

    An introduction to cross compilation with Go
    http://dave.cheney.net/2012/09/08/an-introduction-to-cross-compilation-with-go

  • 相关阅读:
    PHP基础学习笔记(一)
    安装wampserver之后,浏览器中输入localhost页面显示IIS7解决办法
    HTML5常识总结(一)
    AngularJs中的服务
    AngularJs中的directives(指令part1)
    Happy Number——LeetCode
    Binary Tree Zigzag Level Order Traversal——LeetCode
    Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
    Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
    Convert Sorted Array to Binary Search Tree——LeetCode
  • 原文地址:https://www.cnblogs.com/ghj1976/p/3030703.html
Copyright © 2011-2022 走看看