2019.03.03 - Linux搭建go语言交叉环境

编译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
2019.03.03 - Linux搭建go语言交叉环境
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"
 
 
 
 
 
上一篇:页面JS实现按钮点击增加输入框


下一篇:java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)