• golang编译源代码和交叉编译方法



    golang编译源代码和交叉编译方法

    编译源代码

    golang编译其实很简单,下载一份最新的源代码后,进入src目录运行all.bash即可,
    但是你可能会遇到下面问题,这是因为现在golang都是用go语言编译的了,所以你的系统必须先安装好 >= go1.4 的版本.

    ./all.bash 
    Building Go cmd/dist using /home/ailumiyana/go1.4.
    ERROR: Cannot find /home/ailumiyana/go1.4/bin/go.
    Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
    ailumiyana@sola:~/open_src/go/src$ export GOROOT_BOOTSTRAP=/usr/local/go1.4
    

    编译go1.4

    1、先上github上下载一份源代码https://github.com/golang/go

    然后进入目录,查看全部分支。
    可以看到当前最新的release分支是go1.12.
    Alt text

    2、切换到go1.4的分支
    Alt text

    3、进入source目录,运行脚本开始编译

     ~/open_src/go/src$ ./all.bash
    

    4、之后可以进bin目录运行go查看版本 是不是1.4,来确认是否编译成功。

    Alt text

    1. sudo cp go /usr/local/go1.4 -rp 拷贝一份到本地路径
      这样我们就可以使用go1.4编译go1.12了。

    编译go1.12

    1、回到go源码目录我们切换到go1.12分支.
    现在就直接进入src编译是不行的,我们需要先设置go1.4的路径给GOROOT_BOOTSTRAP。

    ./all.bash 
    Building Go cmd/dist using /home/ailumiyana/go1.4.
    ERROR: Cannot find /home/ailumiyana/go1.4/bin/go.
    Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
    ailumiyana@sola:~/open_src/go/src$ export GOROOT_BOOTSTRAP=/usr/local/go1.4
    

    export GOROOT_BOOTSTRAP=/usr/local/go1.4

    2、再次进入src目录编译
    你可能会遇到 _Gidle redeclared in this block 错误
    Alt text

    这是因为我们的上次编译的中间文件没有清理的原因.
    Alt text

    git clean -df

    Alt text

    3、最后编译应该就没问题了,

    Alt text

    交叉编译

    go的交叉编译也比较简便,go env可以看到当前的环境变量配置,
    GOOS、 GOARCH 分别表示目标机系统及处理器体系结构.
    Alt text

    GOOS=linux GOARCH=amd64 go build hello.go

    Alt text

    这些应该是可选搭配 :
    Alt text

  • 相关阅读:
    C++模板的声明与实现分离 编译错误详解
    C语言中数据类型的隐式转换
    UNIX 高手的 10 个习惯
    linux 信号量
    引用与数组
    Linux 的变量命名规则
    .bash_profile和.bashrc的区别(如何设置生效
    URAL 1053 Pinocchio
    URAL 1040 Airline Company
    URAL 1045 Funny Game
  • 原文地址:https://www.cnblogs.com/ailumiyana/p/10735532.html
Copyright © 2020-2023  润新知