ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

34 --> OpenWRT 嵌入式mips环境的go 环境搭建、gin验证测试代码分享

2021-07-24 14:29:34  阅读:627  来源: 互联网

标签:bin home -- robot golang go linux 测试代码 gin


笔者早期在嵌入式上、 http server的程序,实现静态网页相关功能,采用的第三方 goahead 组件,纯C语言实现,
编写应用还是比较麻烦的。
go语音被称作互联网时代的"C" 。简单和高效率,跨平台功能也非常强大,本次项目中再次需要搞嵌入式的WEB服务,
并且还需要提供接口API功能,把设备功能向第三方开放,就采用go 语言来承担此次研发语言。
在工作之余分享一下go语言相关的体会,首先我们需要一个嵌入式开放环境,本篇就作为 go 专栏的开篇文章吧。

第一步 安装 go1.4 编译环境

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

第二步 配置环境变量

为了使编译出来的最新的go支持arm,支持cgo,CC_FOR_TARGET 和 CXX_FOR_TARGET这两个配置项也必须要配置

//配置 go1.4 工作模块
export GOROOT_BOOTSTRAP=/home/robot/golang/go1.4
//配置mips的gcc交叉编译工作目录
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

第三步 安装 go1.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 开启CGO mipsle 指令的编译

编译 g1.11 mips交叉编译环境和 linux-amd64 的环境(宿主机)

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 编译结果查询

宿主机和mips编译结果

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

第四步 配置开发环境变量

4.1 配置开发环境goenv-setup.sh脚本

新建goenv-setup.sh 文件,把下面内容拷贝到 文件中。

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    #开发环境的目录文档
export PATH=$PATH:$GOBIN:GOPATH/bin

4.2 source goenv-setup.sh 初始化go开发环境变量

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

我可以看到 go 环境变量已经添加到 PATH 中,采用这种方式好处是,编译宿主机搭建多种开发环境。

4.3 查看go 版本

robot@ubuntu:~/golang/go-workspace$ go version
go version go1.11 linux/amd64

第五步 编写测试例程并在目标机上验证

5.1 例程

package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}

5.2 交叉编译go 代码

CGO_ENABLED=1 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build hello.go

5.3 目标机上验证

笔者已经验证 交叉编译 结果,此处就不贴图。

总结说明

环境说明:

  • 宿主机 : ubuntu 16
  • 目标主机: mtk7621a , 操作系统 openWRT 19.07

各种平台编译方法列举: golang编译openwrt 程序

GOARCH 可以为 mips/mipsle 分别对应处理器的大小端
GOOS=linux GOARCH=mipsle GOMIPS=softfloat CGO_ENABLED=0 go build

golang编译路由器GL-AR750
GOOS=linux GOARCH=mips GOMIPS=softfloat CGO_ENABLED=0 go build -o xxx xxx.go

golang编译小米路由器R3G

GOOS=linux GOARCH=mipsle GOMIPS=softfloat  go build -o xxx xxx.go

golang编译小米路由器R2D

GOOS=linux GOARM=5 GOARCH=arm CGO_ENABLED=0 go build

golang编译Android

 GOOS=linux GOARCH=arm CGO_ENABLED=0   go build

golang编译Linux

 GOOS=linux GOARCH=amd64  go build

golang编译Windows

 GOOS=windows GOARCH=amd64 CGO_ENABLED=0  go build

golang编译Mac

 GOOS=darwin GOARCH=amd64 CGO_ENABLED=0  go build

缩减包体积

go build -ldflags “-s -w”
-s 的作用是去掉符号信息。去掉符号表,golang panic 时 stack trace 就看不到文件名及出错行号信息了。
-w 的作用是去掉 DWARF tables 调试信息。结果就是得到的程序就不能用 gdb 调试了。

参考链接:
https://rakyll.org/cross-compilation/
https://github.com/golang/go/wiki/Articles#cross-platform-development
https://blog.csdn.net/yyz_1987/article/details/86611750

标签:bin,home,--,robot,golang,go,linux,测试代码,gin
来源: https://blog.csdn.net/weixin_38387929/article/details/119057657

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有