我们使用 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. C#使用Linq to csv读取.csv文件数据

    前言:今日遇到了一个需要读取CSV文件类型的EXCEL文档数据的问题,原本使用NPOI的解决方案直接读取文档数据,最后失败了,主要是文件的类型版本等信息不兼容导致.其他同事有使用linq to csv ...

  2. TensorFlow实现简单线性回归示例代码

    # -*- coding: utf-8 -*- import tensorflow as tf import numpy as np import matplotlib.pyplot as plt d ...

  3. bootstrap-table 常用总结-1

    两种表格工具,今天都用到了,一种是我前几篇写到过的jqgrid,(传送门)另一个就是bootstrap-table了.用过之后会发现,两种表格的方式大同小异,但是为什么这次要换成bootstrap-t ...

  4. Python @classmethod和@staticmethod装饰器使用介绍

    @classmethod和@staticmethod装饰器使用介绍 by:授客 QQ:1033553122 简介 静态方法:类中用 @staticmethod装饰的不带 self 参数的方法.类的静态 ...

  5. SCTF2019 Crypto-warmup writeup

    题外话 其实这道题在比赛过程中并没有解出来,思路完全想偏导致无解就放弃了,后来研究了大佬的writeup大半天才看懂... 正文 nc获取题目信息,返回一段明文和密文,要求输入一段明文和密文. 题目源 ...

  6. MAT分析android内存泄漏

    转载请标明出处:https://www.cnblogs.com/tangZH/p/10955429.html 泄漏,泄漏,漏~ 内存泄漏怎么破,什么是内存泄漏?与内存溢出有什么区别? 内存泄漏(Mem ...

  7. 【iOS】Swipe与Pan区别分析

    By definition, a swipe gesture is necessarily also a pan gesture -- both involve translational movem ...

  8. IDEA新建servlet时出现的错误

    未注入Tomcat里lib文件下的jar 这样即可

  9. Error:Cannot build artifact 'ssm:war exploded' because it is included into a circular dependency (artifact 'ssm:war exploded', artifact 'apinb-master:war exploded')

    打开 File->Project Structure –> Artifacts(ctrl+alt+shift+s) ,这里会有4个,我已经删除了,把 ssm:war 和 ssm:war e ...

  10. appium---模拟点击事件

    在做自动化的过程中都会遇到一些无法定位到的地方,或者通过元素怎么都定位不成功的地方,这个时候我们可以使用必杀技,通过坐标定位.具体的怎么操作呢? swipe点击事件 前面安静写过一篇关于swipe的滑 ...