Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golang 编译到 openwrt 路由器上的坑 #17

Open
shynome opened this issue Mar 17, 2019 · 3 comments
Open

golang 编译到 openwrt 路由器上的坑 #17

shynome opened this issue Mar 17, 2019 · 3 comments
Labels

Comments

@shynome
Copy link
Owner

shynome commented Mar 17, 2019

一开始使用 GOOS=linux GOARCH='mipsle' go build main.go 编译出来的报错:

Illegal instruction

然后找啊找找到了要重新编译内核的时候开启 fpu 之类的, 这可麻烦了, 我压根不懂交叉编译,只能放弃了这种方式, 不过还有一种方法是使用 gomini/go-mips32 进行编译, 不过也是挺麻烦的.

最后找到了这里: golang/go#27236 , 加了一个环境变量就解决了我的问题

GOMIPS=softfloat GOOS=linux GOARCH='mipsle' go build main.go
@shynome shynome added the 杂记 label Mar 17, 2019
@shynome
Copy link
Owner Author

shynome commented Mar 17, 2019

go 跨平台编译真的太舒服了

@shynome
Copy link
Owner Author

shynome commented Mar 17, 2019

使用 build 参数 -ldflags “-s -w” 可以减少约 0.5M 体积哦~
来自这里:
https://blog.lutty.me/code/2017-04/golang-cross-compile-openwrt-ddwrt-build.html
虽然不知道什么原理但是能少点大小挺好的
原理在这:
https://www.golangnote.com/topic/66.html

-s 去掉符号表(这样panic时,stack trace就没有任何文件名/行号信息了,这等价于普通C/C+=程序被strip的效果)
-w 去掉DWARF调试信息,得到的程序就不能用gdb调试了

@shynome
Copy link
Owner Author

shynome commented Aug 16, 2021

加上 CGO_ENABLED=0 就可以编译出 openwrt-x86 可以运行的可执行文件了

这是我用来编译 v2ray 的命令, 没加 CGO_ENABLED=0 之前也是出现 -ash: ./v2ray: not found 的错误, 加上就好了

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o v2ray ./main

参考自该文的第一句话: https://xiazemin.github.io/MyBlog/golang/2019/11/24/libc.html

go build编译时,CGO_ENABLED=1的,自动添加了一些动态库链接,所以编译时吧CGO_ENABLED=0就OK了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant