背景
上一篇文章中,我提到了三种jiao编译golang程序的方式。其中一种是, 在编译openwrt源代码的时候,加入golang的支持。openwrt-go是个不错的选择。这是一个github上的项目。
地址:《GeertJohan/openwrt-go》
一、下载代码
二、编译前的准备
- 自己先安装好编译openwrt所需的一些依赖,包括build-essential,当然了也可以等到编译出错了才逐个安装。
sudo apt-get installgit-core build-essential gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev gawk asciidoc libz-dev libssl-dev
上面的不一定齐全。
2. 安装完之后,要配置一下。
新手麻烦自己看官方文档。《OpenWrt编译系统 – 安装》
3. 最关键的是配置关于golang的内容。
#安装依赖
sudo apt-get install build-essential asciidoc binutils bzip2 gawk gettext git subversion libssl-dev libncurses5-dev patch unzip zlib1g-dev
#下载代码
git clone https://github.com/GeertJohan/openwrt-go
#切换分支
git checkout add-gccgo-and-libgo
#更新软件包列表
./scripts/feeds update -a
#安装软件包
./scripts/feeds install -a
#默认配置
make defconfig
#配置openwrt
make menuconfig
下面的比较重要。
#注意几个选项
-> Advanced configuration options
-> Toolchain options
....
-> Select Build/Install gccgo
....
-> C library implementation
-> Use eglibc
********************************
-> Languages
-> Go
-> golang
三、编译
需要补充一点,我之前两次在ubuntu 14.04上面编译没通过。同样的步骤,在ubuntu16. 04上面一次就通过了。
make V=s
四、最后出来的结果
参考文章:《Golang on OpenWrt》
请根据实际的路径进行调整。
1. golang的路径
export PATH=/home/ubuntu/openwrt-go/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_eglibc-2.19/bin:$PATH
- 链接库
alias mips_gccgo='mips-openwrt-linux-gccgo -Wl,-R,/home/ubuntu/openwrt-go/staging_dir/toolchain-mips_34kc_gcc-4.8.0_eglibc-2.19/lib/gcc/mips-openwrt-linux-gnu/4.8.3 -L /home/ubuntu/openwrt-go/staging_dir/toolchain-mips_34kc_gcc-4.8.0_eglibc-2.19/lib'
- 用法
#helloworld代码
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
编译
mips_gccgo -Wall -o hello_static_libgo hello.go -static-libgo
- 上传到路由器上面运行
http://xzh.i3geek.com
0 条评论