In the early stage, the author realized the functions related to static web pages on embedded and http server programs, using the third-party goahead component and pure C language,
Writing applications is still troublesome.
go voice is called "C" in the Internet age. It is simple and efficient, and its cross platform function is also very powerful. Embedded WEB services are needed again in this project,
It also needs to provide interface API functions to open the device functions to third parties, so go language is used to undertake the R & D language.
After work, let's share the experience related to go language. First of all, we need an embedded open environment. This article will be the opening article of go column.
Step 1 install go1 4 compilation environment
curl -# -O https://dl.google.com/go/go1.4-bootstrap-20171003.tar.gz tar -zxvf go1.4-bootstrap-20171003.tar.gz GOOS=linux GOARCH=amd64 ./make.bash
Step 2: configure environment variables
In order to make the latest go compiled support arm, CGO and CC_ FOR_ Target and cxx_ FOR_ The two configuration items of target must also be configured
//Configure go1 4 working module export GOROOT_BOOTSTRAP=/home/robot/golang/go1.4 //Configure the gcc cross compilation working directory of mips export CC_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gcc export CXX_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-g++ export STAGING_DIR=/home/robot/OpenWrt/mtk7621-19.07/staging_dir
Step 3 install go1 Version 11
curl -# -O https://dl.google.com/go/go1.11.src.tar.gz tar -zxvf go1.11.src.tar.gz cd go1.11/src
3.1 enable compilation of CGO mipsle instruction
Compile G1 11 MIPS cross compilation environment and linux-amd64 environment (host)
robot@ubuntu:~/golang/go1.11/src$ CGO_ENABLED=1 GOOS=linux GOARCH=mipsle GOMIPS=softfloat ./make.bash Building Go cmd/dist using /home/robot/golang/go1.4. Building Go toolchain1 using /home/robot/golang/go1.4. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1. Building Go toolchain2 using go_bootstrap and Go toolchain1. Building Go toolchain3 using go_bootstrap and Go toolchain2. Building packages and commands for host, linux/amd64. Building packages and commands for target, linux/mipsle. --- Installed Go for linux/mipsle in /home/robot/golang/go1.11 Installed commands in /home/robot/golang/go1.11/bin
3.2 query of compilation results
Host and mips compilation results
robot@ubuntu:~/golang/go1.11$ ls pkg/ bootstrap include linux_amd64 linux_mipsle obj tool robot@ubuntu:~/golang/go1.11$ ls bin/ go gofmt linux_mipsle robot@ubuntu:~/golang/go1.11$ ls bin/linux_mipsle/ go gofmt
Step 4 configure development environment variables
4.1 configure the development environment goenv setup SH script
Create a new goenv setup SH file, copy the following contents to the file.
export CC_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gcc export CXX_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-g++ export STAGING_DIR=/home/robot/OpenWrt/mtk7621-19.07/staging_dir export GOROOT=/home/robot/golang/go1.11 export GOBIN=$GOROOT/bin export GOPATH=/home/robot/golang/go-workspace #Catalog document for development environment export PATH=$PATH:$GOBIN:GOPATH/bin
4.2 source goenv-setup.sh initialize go development environment variables
robot@ubuntu:~/golang$ echo $PATH /home/robot/bin:/home/robot/.local/bin:/home/robot/bin:/home/robot/.local/bin:/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin robot@ubuntu:~/golang$ source go-env.sh robot@ubuntu:~/golang$ echo $PATH /home/robot/bin:/home/robot/.local/bin:/home/robot/bin:/home/robot/.local/bin:/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin: /home/robot/golang/go1.11/bin:GOPATH/bin
I can see that the go environment variable has been added to the PATH. The advantage of this method is to compile the host and build a variety of development environments.
4.3 view go version
robot@ubuntu:~/golang/go-workspace$ go version go version go1.11 linux/amd64
Step 5 write the test routine and verify it on the target machine
5.1 routine
package main import "fmt" func main() { fmt.Println("Hello world") }
5.2 cross compiling go code
CGO_ENABLED=1 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build hello.go
5.3 verification on target machine
The author has verified the cross compilation results, so there is no mapping here.
Summary description
Environmental description:
- Host: ubuntu 16
- Target host: mtk7621a, operating system openWRT 19.07
Various platform compilation methods are listed: golang compiles openwrt programs
GOARCH Can be mips/mipsle Corresponding to the size of the processor GOOS=linux GOARCH=mipsle GOMIPS=softfloat CGO_ENABLED=0 go build golang Compile router GL-AR750 GOOS=linux GOARCH=mips GOMIPS=softfloat CGO_ENABLED=0 go build -o xxx xxx.go golang Compile Xiaomi router R3G GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -o xxx xxx.go golang Compile Xiaomi router R2D GOOS=linux GOARM=5 GOARCH=arm CGO_ENABLED=0 go build golang compile Android GOOS=linux GOARCH=arm CGO_ENABLED=0 go build golang compile Linux GOOS=linux GOARCH=amd64 go build golang compile Windows GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build golang compile Mac GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build
Reduce package volume
go build -ldflags "-s -w"
-The function of s is to remove the symbol information. When you remove the stack symbol and the panic file name, you will not see the error.
-w is used to remove the debugging information of DWARF tables. The result is that the resulting program cannot be debugged with gdb.
Reference link:
https://rakyll.org/cross-compilation/
https://github.com/golang/go/wiki/Articles#cross-platform-development
https://blog.csdn.net/yyz_1987/article/details/86611750