编译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. Python可迭代对象中的添加和删除(add,append,pop,remove,insert)

    list: classmates = ['Michael', 'Bob', 'Tracy'] classmates.append('Adam') //添加在末尾,没有add()方法 classmate ...

  2. reactNative 基础

    参考:中文网,极客 一 . 基本程序: import React, { Component } from 'react'; import { Text } from 'react-native'; e ...

  3. MT【267】第一次很重要

    \begin{equation*}\textbf{已知}x_1,x_2<\pi,x_{n+1}=x_n+\left\{ \begin{aligned} sin x_n &,x_n> ...

  4. 【hdu6186】CS Course(前缀后缀异或)

    2017ACM/ICPC广西邀请赛 重现赛1005CS Course 题意 给一个数列a,每次询问去掉第p个的与和.或和,异或和. 题解 预处理前缀和.后缀和即可. 但是当时想都没想就写了个线段树.线 ...

  5. nagios 监控内存使用情况

    监控本机内存cd /usr/lib64/nagios/pluginstouch check_mem.sh #!/bin/bash " ]; then memTotal_b=`free -b ...

  6. 解决mysql配置文件my.cnf添加max_connections不生效

    问题描述: 最新为了方便测试,通过mysql官方指定的yum源安装了mysql5.6.40,在向mysql的配置文件my.cnf添加max_connections=3600后,重启mysql后发现不生 ...

  7. SSH防暴力破解脚本

    crontab -e 编辑添加一下内容 1 1 * * * sh /root/bin/Denyhosts.sh 脚本内容 #!/bin/bash #Denyhosts SHELL SCRIPT #20 ...

  8. CodeForces - 589A(字符串处理)

    题目链接:http://codeforces.com/problemset/problem/589/A 题目大意:给定n个邮件地址,任何电子邮件地址都将显示为“login @ domain”,其中: ...

  9. Ubuntu Server 18.04 网络设置不生效的解决

    在Ubuntu18.04中,传统的配置/etc/network/interfaces已无用https://www.cnblogs.com/dunitian/p/6658578.html 新方法:修改 ...

  10. Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) C D

    C - Maximum Subrectangle 因为是两个数组相乘的到的 矩阵所以  a(i ->j)*b(x->y) 的面积 就是   a(i ->j) 的和乘与b(x-> ...