我们使用 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

GO 使用 动态链接库(共享链接库)进行编译 生成动态链接可执行文件的更多相关文章

  1. gcc/g++ 链接库的编译与链接

    GCC编译步骤 gcc -E t1.c -o t1.i 预处理 gcc -S t1.i -o t1.s 转成汇编语言 gcc -c t1.s -o t1.o 转成机器码 gcc t1.o -o t1. ...

  2. 运维实践-最新Nginx二进制构建编译lua-nginx-module动态链接Lua脚本访问Redis数据库读取静态资源隐式展现

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x0n 前言 ...

  3. Android导入第三方静态库.a编译成动态库.so

    http://ikinglai.blog.51cto.com/6220785/1324985 在Android开发的时候,经常会使用到用c或c++编写的第三方的静态库.如果有源码的话,可以直接跟你自己 ...

  4. 静态库动态库的编译、链接, binutils工具集, 代码段\数据段\bss段解释

    #1. 如何使用静态库 制作静态库 (1)gcc *.c -c -I../include得到o文件 (2) ar rcs libMyTest.a *.o 将所有.o文件打包为静态库,r将文件插入静态库 ...

  5. linux c静态链接库与动态链接库

    库函数是我们编程的时候经常用到的,我们协作编程的时候可以将常用的函数封装成库供大家使用,这样能够提高大家的工作效率.对于库函数,它分为动态链接库和静态链接库.对于静态链接库我们必须是连接到可执行文件中 ...

  6. Linux下动态链接库和静态链接库

    第一部分:编译过程 先了解一下linux下C代码的编译过程,C代码的编译,一般分成四个阶段,包括:预编译,编译,汇编和链接,这四个阶段的分工是 预处理过程,负责头文件展开,宏替换,条件编译的选择,删除 ...

  7. [拾零]C/C++_代码复用的实现_静态链接库_动态链接库_使用.def导出

    1 静态链接库 1.1 创建静态链接库: 1.在VC6中创建项目:Win32 Static Library 2.在项目中创建两个文件:xxx.h 和 xxx.cpp 3.编译 1.2 使用静态链接库 ...

  8. C语言之链接库

    链接库是windows的术语,但对于Linux来说,其概念是一样的.我们通常会把一些相似或相近功能的程序生成链接库,这样的好处是: 1)便于共享,开发软件时如需要相同功能时,不需要将大量重复的代码整合 ...

  9. VC++:创建,调用Win32静态链接库

    概述 DLL(Dynamic Linkable Library)动态链接库,Dll可以看作一种仓库,仓库中包含了可以直接使用的变量,函数或类. 仓库的发展史经历了"无库" ---& ...

随机推荐

  1. 基于MbedTLS的AES加密实现,含STM32H7和STM32F4的实现例程

    说明: 1.mbedTLS的前身是PolarSSL,开源免费. 主要提供了的SSL/TLS支持(在传输层对网络进行加密),各种加密算法,各种哈希算法,随机数生成以及X.509(密码学里公钥证书的格式标 ...

  2. jumpserver 资产管理及授权

    1.用户管理-添加[用户列表] 1.1点击创建用户 1.2创建用户 2.用户管理-添加[用户组] 2.1点击创建用户组 2.2创建用户组   3.资产管理添加资产 3.1添加节点 3.2添加资产(点击 ...

  3. 在iframe 中视频可以正常播放,但是就是不能全屏。解决方法

    iframe标签加上webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="& ...

  4. echarts 双Y轴图表

    直接代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. ABAP分享一 弹出框函数的简单示例

    在开发中经常会使用到弹出框这个功能,在SAP中有很多函数可以实现类似的功能,这里介绍一个比较简单常用的函数  POPUP_TO_CONFIRM 下面是一个实现的简单示例: TABLES sscrfie ...

  6. Ubuntu 18.04安装MySQL

    安装 MySQL 服务端 sudo apt-get install mysql-server 等待安装完成. 检查 mysql 服务状态 servive mysql status 登录 mysql 客 ...

  7. Quartznet速记

    参与 https://www.cnblogs.com/lzrabbit/archive/2012/04/14/2371420.html 1.XML配置 参考:https://www.cnblogs.c ...

  8. log file sync等待超高案例浅析

    监控工具DPA发现海外一台Oracle数据库服务器DB Commit Time指标告警,超过红色告警线(40毫秒左右,黄色告警是10毫秒,红色告警线是20毫秒),如下截图所示,生成了对应的时段的AWR ...

  9. Keepalived集群软件高级使用(工作原理和状态通知)

    1.介绍 Keeaplived主要有两种应用场景,一个是通过配置keepalived结合ipvs做到负载均衡(LVS+Keepalived),有此需求者可参考以往博文:http://lizhenlia ...

  10. [Go]TCP服务中读写进行协程分离

    读写两部分进行一下分离,中间通过chan进行传递数据 ,这样可以方便的在write中进行一些业务处理 single/snet/tcpconn.go package snet import ( &quo ...