编译GO 1.6版本以上的需要依赖GO 1.4版本的二进制,并且需要把GOROOT_BOOTSTRAP的路径设置为1.4版本GO的根目录,这样它的bin目录就可以直接使用到1.4版本的GO

搭建go语言开发环境只需要:

  编译go1.4版本,设置好GOROOT_BOOTSTRAP,然后再执行脚本编译安装GO1.6以上版本

有关资料:

https://www.cnblogs.com/schips/p/10465706.html

 

开发环境介绍

  • 主机操作系统:Ubuntu18.04 64位
  • 目标平台:S5P6818(ARM-A53)
  • 交叉工具链:arm-none-linux-gnueabi-gcc,gcc7.3.0
  • Go版本:1.12  (https://github.com/golang/go/releases)
  • 编译时间:2019.03.03
 

编译Go编译器(Host)

从Go1.4之后Go语言的编译器完全由Go语言编写,所以为了从源代码编译Go需要先编译一个1.4版本的Go版本。
wget -c https://github.com/golang/go/archive/go1.4.2.tar.gz
下载完成之后将得到一个go1.4-bootstrap-20171003.tar.gz压缩包,接下来进行解压编译:
 
sudo tar -xf go-go1.4.2.tar.gz -C /usr/local
cd /usr/local/go-go1.4.2/src
 GOOS=linux GOARCH=amd64 ./make.bash
 
# schips @ ubuntu in /usr/local/go-go1.4.2/src [15:54:23]
$ sudo GOOS=linux GOARCH=amd64 ./make.bash
# Building C bootstrap tool.
cmd/dist
 
 
# Building compilers and Go bootstrap tool for host, linux/amd64.
lib9
/usr/local/go-go1.4.2/src/lib9/fmt/fltfmt.c: In function ‘__efgfmt’:
/usr/local/go-go1.4.2/src/lib9/fmt/fltfmt.c:437:5: error: this statement may fall through [-Werror=implicit-fallthrough=]
   if(ndigits > prec) {
     ^
/usr/local/go-go1.4.2/src/lib9/fmt/fltfmt.c:451:2: note: here
  default:
  ^~~~~~~
cc1: all warnings being treated as errors
go tool dist: FAILED: gcc -Wall -Wstrict-prototypes -Wextra -Wunused -Wno-sign-compare -Wno-missing-braces -Wno-parentheses -Wno-unknown-pragmas -Wno-switch -Wno-comment -Wno-missing-field-initializers -Werror -fno-common -ggdb -pipe -Wuninitialized -O2 -fmessage-length=0 -c -m64 -I /usr/local/go-go1.4.2/include -DPLAN9PORT -I /usr/local/go-go1.4.2/src/lib9 -o $WORK/fltfmt.o /usr/local/go-go1.4.2/src/lib9/fmt/fltfmt.c
 
 
如果出现了 将 警告当成错误的 情况,去掉有关编译脚本中的 -Werror 选项
# schips @ ubuntu in /usr/local/go-go1.4.2/src [15:55:37] C:1
$ grep "Werror" . -nR
sudo vi /usr/local/go-go1.4.2/src/make.bash
sudo vi /usr/local/go-go1.4.2/src/cmd/dist/build.c
sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

设置环境变量

在自己的bash中/etc/bash.bashrc文件中添加如下内容(重启命令行后生效)):
(此时的 go版本是 1.4)
export GOROOT_BOOTSTRAP=/usr/local/go-go1.4.2
export CC_FOR_TARGET=***自己arm-linux-gcc的路径
export CXX_FOR_TARGET=***自己arm-linux-g++的路径
 
 
 
 
编译Go(ARM)
 
完成Go1.4的编译之后,可以利用Go1.4来编译新版本的Go,这里提供
 
tar -zxvf go-go1.12.tar.gz
cd go-go-1.12/src
(下面2条命令2选1)
# 开启CGO编译
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 ./make.bash
# 关闭CGO编译
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 ./make.bash
 
自从golang1.5以后go就使用go语言编译器进行编译了。在golang1.9当中没有使用CGO_ENABLED参数发现依然可以正常编译。当然使用了也可以正常编译。比如把CGO_ENABLED参数设置成1,即在编译的过程当中使用CGO编译器,我发现依然是可以正常编译的。实际上如果在go当中使用了C的库,比如import "C"默认使用go build的时候就会启动CGO编译器,当然我们可以使用CGO_ENABLED=0来控制go build是否使用CGO编译器。
 
这里用到了两个变量:
  • 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
编译其他平台的时候根据上面表格参数执行编译就可以了。
    • $GOARM (for arm only; default is auto-detected if building on the target processor, 6 if not)
      This sets the ARM floating point co-processor architecture version the run-time should target. If you are compiling on the target system, its value will be auto-detected.
      • GOARM=5: use software floating point; when CPU doesn't have VFP co-processor
      • GOARM=6: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
      • GOARM=7: use VFPv3; usually Cortex-A cores
If in doubt, leave this variable unset, and adjust it if required when you first run the Go executable. The GoARM page on the Go community wiki contains further details regarding Go's ARM support.
 
 
经过编译之后go-go1.12目录下会生成arm和amd64两个平台的Go命令和依赖包,所以这个版本编译的Go命令可以进行两个平台的Go应用程序开发。
删除原来的1.4版本,将生成后的新版本的go编译器拷贝到自己的工具链下面
sudo rm /usr/local/go -rf
sudo cp go1.12 /usr/local/go
 
 
在自己的bash中/etc/bash.bashrc文件中添加如下内容(重启命令行后生效)):
export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
 
新开一个bash,输入 go version 可以进行安装的简单验证
$ go version
go version go1.12 linux/amd64
 
 
新建helloworld.go
package main
import "fmt"
func main() {
    fmt.Println("Hello world")
}
 
go build helloworld.go
./helloworld
 
 
$ GOOS=linux GOARCH=arm GOARM=7 go build  helloworld.go
$ ./helloworld
zsh: exec format error: ./helloworld
 
 
 
#For swtich the cross cmoplier
ARMLINUXGCC="XILINX"
ARMLINUXGCC="ARM-2009"
#ARMLINUXGCC="ARM-2014"
#ARM_GCC_PATH
if [ $ARMLINUXGCC = "XILINX" ]
then
    ARM_GCC_PATH="/usr/arm-xilinx-linux-gnueabi/bin"
else if [ $ARMLINUXGCC = "ARM-2014" ]
then
    ARM_GCC_PATH="/usr/local/arm/arm-2014.05/bin"
else
    ARM_GCC_PATH="/usr/local/arm/arm-2009q3/bin"
    fi
fi
export PATH=$PATH:$ARM_GCC_PATH
echo -e "GCC :\033[33m $ARMLINUXGCC \033[0m"
export PATH=$PATH:/usr/local/arm/gcc-3.4.5-glibc-2.3.6/bin
export PATH=$PATH:/usr/local/arm/gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-gnueabi/bin
 
 
#For Golang
if [ $ARMLINUXGCC = "XILINX" ]
then
    GO_CC_FOR_TARGET="$ARM_GCC_PATH/arm-xilinx-linux-gnueabi-gcc"
    GO_CXX_FOR_TARGET="$ARM_GCC_PATH/arm-xilinx-linux-gnueabi-g++"
else
    GO_CC_FOR_TARGET="$ARM_GCC_PATH/arm-none-linux-gnueabi-gcc"
    GO_CXX_FOR_TARGET="$ARM_GCC_PATH/arm-none-linux-gnueabi-g++"
fi
export GOROOT_BOOTSTRAP=/usr/local/go
export CC_FOR_TARGET=$GO_CC_FOR_TARGET
export CXX_FOR_TARGET=$GO_CXX_FOR_TARGET
export GOROOT=/usr/local/arm/go
export GOPATH=/usr/local/arm/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
alias arm-go="GOOS=linux GOARCH=arm GOARM=7 go build"
alias gob="go build"
 
 
 
 
 

2019.03.03 - Linux搭建go语言交叉环境的更多相关文章

  1. 从零开始搭建Go语言开发环境

    一步一步,从零搭建Go语言开发环境. 安装Go语言及搭建Go语言开发环境 下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://gol ...

  2. 安装Go语言及搭建Go语言开发环境

    一步一步,从零搭建Go语言开发环境. 安装Go语言及搭建Go语言开发环境 下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://gol ...

  3. GO学习-(2) 从零开始搭建Go语言开发环境

    从零开始搭建Go语言开发环境 一步一步,从零搭建Go语言开发环境. 安装Go语言及搭建Go语言开发环境 下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站( ...

  4. SublimeText3搭建go语言开发环境(windows)

    SublimeText3搭建go语言开发环境(windows) 下载并解压:     Sublime Text Build 3021.zip注册:     尽量不要去破解    安装Package C ...

  5. Windows10下配置Linux下C语言开发环境

    今天为大家介绍如在Windows10下配置Linux下C语言开发环境,首先安装linux子系统:启用开发者模式 1.打开设置 2.点击更新和安全3.点击开发者选项 4.启用开发人员模式 5.更改系统功 ...

  6. Win7下搭建Go语言开发环境

    Win7下搭建Go语言开发环境 1 下载适合window版本的Go安装包,下载地址http://code.google.com/p/go/downloads/list 2 下载适合window本本的L ...

  7. 干货分享:在Windows下使用Visual Studio搭建C语言开发环境

    前言:本文将教大家如何使用 VIsual Studio Code 搭建 C 语言开发环境,包括使用 VS Code 如何编译和调试 C 语言程序,需要 用到的工具有 Visual Studio Cod ...

  8. 实验四 Linux系统搭建C语言编程环境

    项目 内容 这个作业属于那个课程 <班级课程的主页链接> 这个作业的要求在哪里 <作业要求链接地址> 学号-姓名 17043220-万文文 作业学习目标 1).Linux系统下 ...

  9. AbelSu教你搭建go语言开发环境

    go语言官网:https://golang.org/ windows:官网下载go1.6.windows-amd64.msi安装文件,安装位置选择默认C:\Go\安装结束后配置环境变量Path: C: ...

随机推荐

  1. shelve 模块

    shelve 模块概述:   shelve是python的自带model.   可以直接通过import shelve来引用.   shelve类似于一个存储持久化对象的持久化字典,即字典文件.   ...

  2. Spring01-Ioc基本使用

    一. Spring简介 1. Spring介绍 Spring框架主页: Spring官网 Spring资源地址:下载地址 Spring框架,由Rod Johnson开发 Spring是一个非常活跃的开 ...

  3. 「Link-Cut Tree」学习笔记

    Link-Cut Tree,用来解决动态树问题. 宏观上,LCT维护的是森林而非树.因此存在多颗LCT.有点像动态的树剖(链的确定通过$Access$操作),每条链用一颗$splay$维护.$spla ...

  4. python中的map函数

    def f(x): return x * x """将一个全是数字的list变成平方形式""" def f2(): ls = [1, 2, ...

  5. bzoj 1452: [JSOI2009]Count (二维树状数组)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1452 思路: 对每个颜色开一个二维树状数组维护就好了 实现代码: #include<b ...

  6. zabbix agent 自定义 UserParameter

    有时候我们想让被监控端执行一个zabbix没有预定义的检测,zabbix的用户自定义参数功能提供了这个方法.我们可以在客户端配置文件zabbix_angentd.conf里面配置UserParamet ...

  7. CDQ分治与整体二分学习笔记

     CDQ分治部分 CDQ分治是用分治的方法解决一系列类似偏序问题的分治方法,一般可以用KD-tree.树套树或权值线段树代替. 三维偏序,是一种类似LIS的东西,但是LIS的关键字只有两个,数组下标和 ...

  8. scrapy 选择器

    1.lxml(转自简书) from lxml import etree2 import requests3 45 url = " "6 html = requests.get(ur ...

  9. nginx+腾讯云免费ssl证书+阿里云ECS实现Https配置

    1. 申请SSL证书: 首先我们需要到腾讯云那边申请一个ssl证书,对于个人博客类型的,建议采用免费版本,土豪除外 申请地址:https://console.cloud.tencent.com/ssl ...

  10. Java线程池中submit()和execute之间的区别?

    一: submit()方法,可以提供Future < T > 类型的返回值. executor()方法,无返回值. execute无返回值 public void execute(Runn ...