我们使用 go help buildmode 可以看到 go 可以以多种方式进行构建,默认使用静态链接库.

➜  src go help buildmode
The 'go build' and 'go install' commands take a -buildmode argument which
indicates which kind of object file is to be built. Currently supported values
are: -buildmode=archive
Build the listed non-main packages into .a files. Packages named
main are ignored. -buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable symbols will be those
functions exported using a cgo //export comment. Requires
exactly one main package to be listed. -buildmode=c-shared
Build the listed main package, plus all packages it imports,
into a C shared library. The only callable symbols will
be those functions exported using a cgo //export comment.
Requires exactly one main package to be listed. -buildmode=default
Listed main packages are built into executables and listed
non-main packages are built into .a files (the default
behavior). -buildmode=shared
Combine all the listed non-main packages into a single shared
library that will be used when building with the -linkshared
option. Packages named main are ignored. -buildmode=exe
Build the listed main packages and everything they import into
executables. Packages not named main are ignored. -buildmode=pie
Build the listed main packages and everything they import into
position independent executables (PIE). Packages not named
main are ignored. -buildmode=plugin
Build the listed main packages, plus all packages that they
import, into a Go plugin. Packages not named main are ignored.

GO buildmode

在macos上我们使用shared 模式,但是显示不支持,我们换成linux平台进行实验:

➜  src go install -buildmode=shared yxpkg
-buildmode=shared not supported on darwin/amd64

创建libstd.so 库:

root@docker ~/go# go install -buildmode=shared std

创建yxpkg包的 so库:

root@docker ~/go# go install -buildmode=shared -linkshared yxpkg

编译 main.go 生成动态链接的可执行文件:

root@docker ~/g/src# go build -linkshared yaoxu.go

我们对比之前生成的静态链接的可执行文件:发现其可执行文件大小,相差很大;

root@docker ~/g/src# ll
total 1.9M
-rwxr-xr-x. root root 22K Aug : yaoxu*
-rw-r--r--. root root Aug : yaoxu.go
drwxr-xr-x. root root .0K Aug : yxpkg/
-rwxr-xr-x. root root 1.9M Aug : yx_static*

我们分别使用ldd 查看两个文件:

可见,两个文件一个是动态链接文件,一个是静态链接文件。

其中需要注意的是,go进行动态链接编译的时候,还是需要源代码文件辅助编译,我想主要是构建符号表的原因。

还有一些具体的细节,你可以配置自己的环境,自行进行测试;

编译后的工作区的目录结构如下:

其中,yxpkg 是包,yaoxu.go文件中使用到了 yxpkg包中的函数内容;

工作区代码可以在如下连接中找到:https://github.com/yaowenxu/Workplace/tree/master/go

保持更新,如果对您有帮助,请关注 cnblogs.com/xuyaowen

最新文章

  1. C语言 · 删除数组0元素
  2. 浅谈UIAlertController使用
  3. Coursera台大机器学习课程笔记6 -- The VC Dimension
  4. Bash:-:-定义空变量作为输出结合换行符\n和column输出
  5. CentOS离线安装GCC编译环境
  6. html常用的综合体
  7. PHP Filter
  8. Webview 中 Javascript 无法调用 Java 对象
  9. 实现一个类 Vue 的 MVVM 框架
  10. 如何使用Node.js编写命令工具——以vue-cli为例
  11. WEB服务器防盗链_HttpAccessKeyModule_Referer(Nginx&&PHP)
  12. Vue 进阶之路(五)
  13. swift4.0 数据转模型
  14. Flash Alternativa 3D引擎-基础理论
  15. 去重和分类后缀asp、php等路径 用python3写的
  16. GitLab安装及使用
  17. zabbix3.4.7常用监控项
  18. Matplotlib常用绘图示例
  19. 洛谷.1782.旅行商的背包(背包DP 单调队列)
  20. 前端框架之Vue(7)-事件处理

热门文章

  1. redis集群搭建及常用操作
  2. Hive concat函数连接后结果为null
  3. IT兄弟连 HTML5教程 CSS3属性特效 盒模型阴影
  4. 前端开发规范:4-JS
  5. 【Kafka】《Kafka权威指南》——提交和偏移量
  6. VS2017无法打开Razor视图文件提示:引发类型"System.Exception"异常
  7. 获取Tekla属性方式
  8. 工具-Xmind常用快捷键/使用
  9. JMS消息中间件
  10. [Go] golang定时器的使用