Development environment introduction
- Host operating system: Ubuntu14.04 64-bit
- Target platform: IMX.6Q ARM Cortex-A9 core
- Cross tool chain: arm-poky-linux-gnueabi, gcc4.9.1
- Go version: go1.13
- Compile time: 2019.10.26
Install Go1.4 on the host system
Download the source code of go1.4 first. Because go language version compilers after go1.4 version are written in go language, use go language to compile go, go1.4 is the last version compiled with gcc. So in order to compile Go from source code, you need to compile a 1.4 version of Go first.
Download link of go1.4-bootstrap: mirrors.nju.edu.cn/golang/go1.4-bootstrap-20171003.tar.gz
After the download is complete, you will get a go1.4-bootstrap-20171003.tar.gz compressed package, then unzip, compile and install:
xxx@ubuntu:~$ export GO_INSTALL_PATH=/usr/local
xxx@ubuntu:~$ sudo tar -zxvf go1.4-bootstrap-20171003.tar.gz -C $GO_INSTALL_PATH
xxx@ubuntu:~$ cd $GO_INSTALL_PATH/go/src
xxx@ubuntu:/usr/local/go/src$ sudo GOOS=linux GOARCH=amd64 ./make.bash
After compilation, the go command binary file will be generated in the $GO_INSTALL_PATH/go/bin directory. View the version of go
xxx@ubuntu:~$ $GO_INSTALL_PATH/go/bin/go version
go version go1.14 linux/amd64
Compile the target platform Go (ARM)
After completing the compilation of Go1.4, you can use Go1.4 to compile the new version of Go. Here is the download address of the Go source code
下载地址:mirrors.nju.edu.cn/golang/go1.13rc2.src.tar.gz
Unzip the source code:
# 设置编译目录
xxx@ubuntu:~$ export GO_BUILD=~/go_build
# 创建编译目录
xxx@ubuntu:~$ mkdir $GO_BUILD
# 解压源码到编译目录
xxx@ubuntu:~$ tar -xvf go1.13rc2.src.tar.gz -C $GO_BUILD
# 进入源码目录
xxx@ubuntu:~$ cd $GO_BUILD/go/src
Set environment variables
Compilation related environment variable description:
parameter | Description |
GOROOT_FINAL | Go language root directory (optional) |
GOHOSTARCH | Host compiler system architecture |
GOARCH | Host system architecture |
GOOS | Host operating system |
GO_GCFLAGS | Go tool compilation parameters when building packages and commands |
GO_LDFLAGS | Go tool compilation parameters when building commands |
CC | Host C compiler |
CC_FOR_TARGET | Target system C compiler |
CXX_FOR_TARGET | Target system C++ compiler |
PKG_CONFIG | Path of pkg-config tool |
GOROOT_BOOTSTRAP | installation manual |
GOARM | The version number of the floating-point arithmetic coprocessor used is only useful for the arm platform. The optional values are 5, 6, 7 |
Settable values of GOOS GOARCH:
GOOS | GOARCH |
darwin | 386 |
darwin | amd64 |
dragonfly | 386 |
dragonfly | amd64 |
freebsd | 386 |
freebsd | amd64 |
freebsd | arm |
linux | 386 |
linux | amd64 |
inux | arm |
netbsd | 386 |
netbsd | amd64 |
netbsd | arm |
openbsd | 386 |
openbsd | amd64 |
plan9 | 386 |
plan9 | amd64 |
solaris | amd64 |
windows | 386 |
windows | amd64 |
Create arm_gcc.sh and arm_g++.sh files in $GO_BUILD/go/src:
hegaozhi@ubuntu:~/go_build/go/src$ cd $GO_BUILD/go/src
# 创建文件
hegaozhi@ubuntu:~/go_build/go/src$ touch arm_gcc.sh arm_g++.sh
Write the corresponding C cross compiler information in the arm_gcc.sh file:
#!/bin/sh
exec /opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 --sysroot=/opt/poky/1.7/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi "$@"
Write the corresponding C++ cross compiler information in the arm_g++.sh file:
#!/bin/sh
exec /opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ -march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 --sysroot=/opt/poky/1.7/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi "$@"
Enter the following in the terminal to set environment variables:
# 个人交叉编译器信息,非必须
export SDKTARGETSYSROOT=/opt/poky/1.7/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi
export PATH=/opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/bin:/opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH=$SDKTARGETSYSROOT/usr/lib/pkgconfig
export CONFIG_SITE=/opt/poky/1.7/site-config-cortexa9hf-vfp-neon-poky-linux-gnueabi
export OECORE_NATIVE_SYSROOT="/opt/poky/1.7/sysroots/x86_64-pokysdk-linux"
export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"
export OECORE_ACLOCAL_OPTS="-I /opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/share/aclocal"
export PYTHONHOME=/opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr
#1.4版本的安装路径
export GOROOT_BOOTSTRAP=$GO_INSTALL_PATH/go
# 目标平台C代码编译器信息
export CC_FOR_TARGET=$GO_BUILD/go/src/arm_gcc.sh
# 目标平台C++代码编译器信息
export CXX_FOR_TARGET=$GO_BUILD/go/src/arm_g++.sh
# 目标平台编译器参数,非必须
export GOGCCFLAGS='-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 --sysroot=/opt/poky/1.7/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi -O2 -pipe -g -feliminate-unused-debug-types -fPIC -m64 -fmessage-length=0'
Compile:
hegaozhi@ubuntu:~/go_build/go/src$ CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 ./make.bash
Tip: When you choose to enable CGO compilation, you must configure the two environment variables CC_FOR_TARGET and CXX_FOR_TARGET
After compilation, Go commands and dependency packages for arm and amd64 platforms will be generated in the $GO_BUILD/go directory, so the Go commands compiled in this version can be used to develop Go applications for both platforms.
Replace the old Go1.4 with the newly compiled Go1.13 version:
cd $GO_BUILD
sudo rm $GO_INSTALL_PATH/go -rf
sudo cp -r go $GO_INSTALL_PATH/
Set running environment variables
Add the following content to the /etc/bash.bashrc file (take effect after restarting the command line)):
export GOROOT_BOOTSTRAP=/usr/local/go
# 为了可以编译CGO的Go应用程序(ARM版本)必须要保留下面的两个环境变量
export CC_FOR_TARGET=$GOROOT_BOOTSTRAP/src/arm_gcc.sh
export CXX_FOR_TARGET=$GOROOT_BOOTSTRAP/src/arm_g++.sh
export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Verify Go
# 查看版本
xxx@ubuntu:~$ go version
go version go1.10.3 linux/amd6
# 查看参数
xxx@ubuntu:~$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/hegaozhi/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/gopath"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build791570790=/tmp/go-build -gno-record-gcc-switches"
Compile the Helloworld program, create a new helloworld.go file, and fill in the following content
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
Compile the ARM version application
xxx@ubuntu:~$ CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 go build helloworld.go
After the compilation is complete, helloworld will be generated in the current directory, and this file will be uploaded to the ARM target file system for testing.
View the properties of the generated helloworld file:
xxx@ubuntu:~/golang$ file helloworld
helloworld: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
Examples of golang tuning C on the Internet
https://download.csdn.net/download/hgz_gs/12084628