golang中怎么编译二进制运行文件
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,今天就跟大家聊聊有关golang中怎么编译二进制运行文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。系统包含 sqlite 包的用普通编译会
千家信息网最后更新 2026年11月03日golang中怎么编译二进制运行文件
今天就跟大家聊聊有关golang中怎么编译二进制运行文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
系统包含 sqlite 包的用普通编译会出现异常
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o $file_path . #编译liunx系统可运行文件,会报错
报以下错误
github.com/mattn/go-sqlite3../../mattn/go-sqlite3/sqlite3_opt_preupdate.go:12:16: undefined: SQLiteConn
这时应该
CGO_ENABLED=0 改成 CGO_ENABLED=1
下一步运行,如果报
# os/user/usr/local/go/src/os/user/getgrouplist_unix.go:16:35: warning: passing 'gid_t *' (aka 'unsigned int *') to parameter of type 'int *' converts between pointers to integer types with different sign [-Wpointer-sign]/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/unistd.h:653:43: note: passing argument to parameter here# github.com/mattn/go-sqlite3sqlite3-binding.c:32993:42: error: use of undeclared identifier 'pread64'sqlite3-binding.c:33011:42: error: use of undeclared identifier 'pwrite64'sqlite3-binding.c:33143:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'sqlite3-binding.c:33152:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'sqlite3-binding.c:33179:20: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'sqlite3-binding.c:33196:16: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'sqlite3-binding.c:14168:38: note: expanded from macro 'ArraySize'sqlite3-binding.c:33200:14: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'sqlite3-binding.c:14168:38: note: expanded from macro 'ArraySize'sqlite3-binding.c:35853:11: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]sqlite3-binding.c:32997:49: note: expanded from macro 'osPread64'sqlite3-binding.c:35965:17: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]sqlite3-binding.c:33015:57: note: expanded from macro 'osPwrite64'
该问题 Mac未安装linux的交叉编译器。解决方法,安装linux的交叉编译器
下载地址
Compiling for Linux 32 and Linux 64 on MacOS X
修改编译命令为
CGO_ENABLED=0 GOOS=linux CC=/usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-gcc go build -a -installsuffix cgo -o $file_path .#cc路径请下载时看清楚安装包的默认路径填写
一般编译出来的文件已经可以在liunx上运行了,但是如果放在docker上的 alpine系统上又会出现问题会无法执行问题
这个问题一般是alpine没有对应的依赖库可以用下面的方法解决
查看下执行文件的依赖库:
ldd $WORKDIR/main/lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
直接系统操作
mkdir /lib64ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
对应dockerfile里的操作
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
然后就可以正常工作了,原理是musl和glibc是兼容的,通过创建该符号链接修复缺少的依赖项。alpine这个5M的镜像也能满足go二进制文件的运行环境!
对于用alpine作为go的编译环境同样存在上述问题,同样用相同方法可以解决。
看完上述内容,你们对golang中怎么编译二进制运行文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
编译
文件
运行
问题
系统
二进制
内容
方法
环境
编译器
路径
交叉
普通
相同
原理
命令
地址
更多
果报
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
科技赋能下的互联网金融
科技股互联网
互联网下的科技发展
互联网与科技发展
紫鲸互联网科技
广州宏创互联网科技
远拓互联网科技
南京欣网互联网科技
沃里互联网科技
山东吉时兔互联网科技有限公司
富国互联网科技创新基金
银行科技岗属于互联网吗
江苏互联网智能科技软件
吉林省码上互联网科技电话
容联科技互联网电商
银川新华互联网科技学校原画
山东蛋芒互联网科技有限公司
北京品多多互联网科技有限公司
优雇互联网科技有限公司
株洲百竞科技的互联网营销顾问
精品咖啡豆
互联网科技公司经营范围
青岛互联网科技公司
奇士达互联网科技
现代互联网科技股
互联网科技和信息
科技互联网的核心竞争力
锡山区互联网智能科技产品供应
智帮互联网科技公司最近动向
同城互联网络科技