golang中怎么编译二进制运行文件

发布时间:2021-06-15 14:30:09 作者:Leah
来源:亿速云 阅读:376

今天就跟大家聊聊有关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-sqlite3

sqlite3-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 /lib64

ln -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中怎么编译二进制运行文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 怎么编译并运行golang程序
  2. golang编译的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang

上一篇:ubuntu16.04中怎么安装kibana

下一篇:Java为什么支持方法重载

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》