Ubuntu下交叉编译kcptun go语言源码 for openwrt
1、安装go 1.8环境
golang从1.8开始官方支持mips32了,执行效率比之前非官方版gomini的快很多
以下操作以root用户执行,如以其他用户执行,需修改为对应路径
下载版本:
git clone https://go.googlesource.com/go
cd go
要安装1.8版本,先要安装1.4版本,不然会出错
git checkout go1.4
安装go 1.4:
cd src
./all.bash
export GOROOT_BOOTSTRAP=~/go1.4
cp ~/go ~/go1.4 -rf
清除环境
cd ~/go
git clean -dfx
安装go 1.8:
git checkout go1.8beta2
cd src
./all.bash
安装成功后会提示:
加入路径
export PATH=$PATH:~/go/bin
#go version
go version devel +9cd3c06 Thu Dec 15 20:06:07 2016 +0000 linux/386
设置profile
echo 'export PATH=$PATH:~/go/bin' >> /etc/profile
2、编译kcptun
设置环境变量
export GOARCH=mips
export GOPATH=~/go/src/gocode
export GOOS=linux
export GOROOT=~/go
下载必要的包,需确认下载正确
go get -v golang.org/x/crypto/pbkdf2
go get -v github.com/xtaci/kcp-go
go get -v github.com/urfave/cli
go get -v github.com/hashicorp/yamux
go get -v github.com/golang/snappy
go get -v golang.org/x/net/ipv4
go get -v github.com/xtaci/kcptun/client
go get -v github.com/xtaci/kcptun/server
go get -v github.com/klauspost/cpuid
开始编译
cd ~
git clone https://github.com/xtaci/kcptun.git
cd kcptun
./build-release.sh
或者按如下方式编译
VERSION=`date -u +%Y%m%d`
LDFLAGS="-X main.VERSION=$VERSION -s -w"
env CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "$LDFLAGS" -o client_linux_mips github.com/xtaci/kcptun/client
编译完成:
编译后的软件如果在openwrt上运行出现如下错误:
root@wndr3800:/usr/bin/kcptun# ./client_linux_mips
Illegal instruction
那是因为内核中没有开启FPU
开启方法:
在openwrt的编译目录执行:
make kernel_menuconfig
选中上述选项后重新编译
或者也可以用gomini编译kcptun,不需要开FPU
gomini 1.4版本编译过程:
首先要看你使用的路由器cpu架构,是big还是little
https://dev.openwrt.org/wiki/platforms
big用mips,little用mipsel,ar71xx是big,小米、极路由等MT76xx是little
little编译参考:
https://github.com/xtaci/kcptun/issues/79
ar71xx编译过程:
git clone https://github.com/gomini/go-mips32.git
cd go-mips32/src
export GOOS=linux
export GOARCH=mips32
./make.bash
mkdir ~/kcp
cd ~/kcp
export GOPATH=~/go-mips32/src/gocode
export GOOS=linux
export GOARCH=mips32
export GOROOT=~/go-mips32
export PATH=~/go-mips32/bin:$PATH
go get -v golang.org/x/crypto/pbkdf2
go get -v github.com/xtaci/kcp-go
go get -v github.com/urfave/cli
go get -v github.com/hashicorp/yamux
go get -v github.com/golang/snappy
go get -v golang.org/x/net/ipv4
到这一步我这里提示:
ywb@ubuntu:~/kcp$ go get -v golang.org/x/net/ipv4
golang.org/x/net/ipv4
# golang.org/x/net/ipv4
../go-mips32/src/gocode/src/golang.org/x/net/ipv4/zsys_linux_ppc.go:7: sysIP_TOS redeclared in this block
previous declaration at ../go-mips32/src/gocode/src/golang.org/x/net/ipv4/zsys_linux_mips.go:7
定义重复,临时解决方法(没时间深究原因):
将此目录(~/go-mips32/src/gocode/src/golang.org/x/net/ipv4/)下除了zsys_linux_mips.go外的所有zsys_*_*.go名称改成zsys_*_*.go1:
再执行go get -v golang.org/x/net/ipv4成功,继续
go get -v github.com/xtaci/kcptun/client
go get -v github.com/xtaci/kcptun/server
VERSION=`date -u +%Y%m%d`
LDFLAGS="-X main.VERSION=$VERSION -s -w"
env CGO_ENABLED=0 GOOS=linux GOARCH=mips32 go build -ldflags "$LDFLAGS" -o client_linux_mips github.com/xtaci/kcptun/client
env CGO_ENABLED=0 GOOS=linux GOARCH=mips32 go build -ldflags "$LDFLAGS" -o server_linux_mips github.com/xtaci/kcptun/server
搞定收工
发表评论