news 2026/3/31 15:44:16

Linux动静态库以及动静态链接

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux动静态库以及动静态链接

ar 是 gnu 归档⼯具(打包), rc 表⽰ (replace and create)

打包成静态库: ar -rc libXXX.a *.o

代码语言:javascript

// Makefile libmystdio.a:my_stdio.o my_string.o @ar -rc @^ %.o:%.c @gcc -c

在 Makefile 中,命令前的@是 静默执行标记,核心作用是:隐藏命令本身的输出,只显示命令执行后的结果(或自定义echo提示)

用法

效果

适用场景

命令前加 @

隐藏命令本身,只显示执行结果/echo提示

绝大多数构建/清理/打包命令

命令前不加 @

显示命令本身 + 执行结果

调试 Makefile 时(查看实际执行的命令)

ar -tv libmystdio.a • t: 列出静态库中的⽂件 • v:verbose 详细信息

静态库的使用

程序中使用到库的内容时,需要将库进行链接。

Linux查找库的时候只会区lib64路径下去查。其他路径需要加路径

代码语言:javascript

// 场景1:头⽂件和库⽂件安装到系统路径下 gccmain.c−l库名字//场景2:头⽂件和库⽂件和我们⾃⼰的源⽂件在同⼀个路径下 gcc main.c -L. -l库名字 // 场景3:头⽂件和库⽂件有⾃⼰的独⽴路径

• -L: 指定库路径

• -I: 指定头⽂件搜索路径

• -l: 指定库名

• 测试⽬标⽂件⽣成后,静态库删掉,程序照样可以运⾏

• 关于 -static 选项,稍后介绍

• 库⽂件名称和引⼊库的名称:去掉前缀 lib ,去掉后缀 .so ,.a ,如: libc.so -> c

gcc专门编译C语言,默认认识C标准库,所以不用显示使用-l。gcc默认区/lib64下查找库,所以标准库不用-L。

库不可以包含main函数!!

我给别人提供一个库,要提供: 1. .a库文件 2. .h头文件(你的库的使用手册)

使用别人的库: (在使用任何三方库时必须使用-l)

  1. 可以将头文件拷贝到/usr/include/下,库文件拷贝到/lib64/下。这就是安装的过程。 此时,在使用时,必须使用-l指明使用的是哪一个库。
  2. 使用选项-L -I -l指明条件,来使用静态库。头文件在预处理阶段就需要。
  3. 也可以在/lib64/下,与别人的库进行软链接。库文件拷贝到/lib64/下。使用-l找到别人的库
动态库

链接时,要使用-shared

生成.o文件时,要是用fPIC

代码语言:javascript

// Makefile libmystdio.so:my_stdio.o my_string.o gcc -o @^ -shared %.o:%.c gcc -fPIC -c

• shared:表⽰⽣成共享库格式

• fPIC:产⽣位置⽆关码(positionindependentcode)

• 库名规则:libxxx.so

  1. 形成动态库,不使用ar,而是使用gcc,g++。可以知道,默认形成的是动态库,形成静态库要使用附加命令。
动态库的使用

库的名字要去掉前缀和后缀

ldd libXXX.so // 查看库或者可执⾏程序的依赖

代码语言:javascript

// 场景1:头⽂件和库⽂件安装到系统路径下 gccmain.c−l库文件名//场景2:头⽂件和库⽂件和我们⾃⼰的源⽂件在同⼀个路径下 gcc main.c -L. -l库文件名 // 从左到右搜索-L指定的⽬录 // 场景3:头⽂件和库⽂件有⾃⼰的独⽴路径

只使用gcc main.c -I头⽂件路径 -L库⽂件路径 -lmymath时,执行可执行程序会有找不到库的情况。

这一步,是告诉了gcc我的库信息,是给编译器说的

执行可执行程序的时候,会有找不到库的情况。这时是操作系统(加载器)找不到库。

使用静态库的时候没这个问题,是由于静态库当中的方法,拷贝到了我的程序内部。程序运行,就不需要库了。

动态库加载时的查找问题(运行时的查找问题)
  1. 将库文件拷贝到/lib64路径下

(拷⻉ .so ⽂件到系统共享库路径下,⼀般指 /usr/lib、/usr/local/lib、/lib64 或者开 篇指明的库路径等 )

  1. 将库文件,在/lib64/下的文件中,建立软链接(向系统共享库路径下建⽴同名软连接)

ln -s 路径 /lib64/lib库名.so

1 2方法可以认为本质是一样的。

  1. 更改环境变量: LD_LIBRARY_PATH

动态搜索路径除了搜索/lib64还会在该环境变量中寻找。将库的路径导入其中。

LD_LIBRARY_PATH = $ LD_LIBRARY_PATH + 路径

  1. ldconfig⽅案:配置/ etc/ld.so.conf.d/ ,ldconfig更新

代码语言:javascript

[root@localhost linux]# cat /etc/ld.so.conf.d/bit.conf /root/tools/linux [root@localhost linux]# ldconfig // 要⽣效,这⾥要执⾏ldconfig,重新加载库搜索路径

在/etc/ld.so.conf.d/下新建文件,将你的库的路径拷贝进去。再执行ldconfig命令

静态链接(Static Linking)
1. 原理

静态链接在**编译阶段**将所有依赖的库代码复制到最终的可执行文件中。

  • 使用的库文件:.a(静态库)
  • 链接器:ld(由gcc调用)


https://www.dongchedi.com/article/7595659395523379774
https://www.dongchedi.com/article/7595659395523314238
https://www.dongchedi.com/article/7595659660309365273
https://www.dongchedi.com/article/7595659111317340696
https://www.dongchedi.com/article/7595659730602017342
https://www.dongchedi.com/article/7595660394555867672
https://www.dongchedi.com/article/7595659558165430808
https://www.dongchedi.com/article/7595657885754442265
https://www.dongchedi.com/article/7595658483920618046
https://www.dongchedi.com/article/7595658759406387737
https://www.dongchedi.com/article/7595657988032217624
https://www.dongchedi.com/article/7595659157458600473
https://www.dongchedi.com/article/7595657304452661784
https://www.dongchedi.com/article/7595656794677199384
https://www.dongchedi.com/article/7595657881354011161
https://www.dongchedi.com/article/7595657881354142233
https://www.dongchedi.com/article/7595657751876223513
https://www.dongchedi.com/article/7595657821845520958
https://www.dongchedi.com/article/7595658051340845592
https://www.dongchedi.com/article/7595655635686752830
https://www.dongchedi.com/article/7595663911891386942
https://www.dongchedi.com/article/7595657130259399230
https://www.dongchedi.com/article/7595655593827140120
https://www.dongchedi.com/article/7595685207777542680
https://www.dongchedi.com/article/7595684262021022232
https://www.dongchedi.com/article/7595683141235753497
https://www.dongchedi.com/article/7595684711583023640
https://www.dongchedi.com/article/7595681945678463513
https://www.dongchedi.com/article/7595683732863107608
https://www.dongchedi.com/article/7595681322010837529
https://www.dongchedi.com/article/7595683141235556889
https://www.dongchedi.com/article/7595681143517839897
https://www.dongchedi.com/article/7595681023242207769
https://www.dongchedi.com/article/7595681960798847512
https://www.dongchedi.com/article/7595680700909486654
https://www.dongchedi.com/article/7595681102904377918
https://www.dongchedi.com/article/7595680303315223102
https://www.dongchedi.com/article/7595668990530617881
https://www.dongchedi.com/article/7595668797815013950
https://www.dongchedi.com/article/7595669006708097561
https://www.dongchedi.com/article/7595669797971853848
https://www.dongchedi.com/article/7595669549916160574
https://www.dongchedi.com/article/7595670355927286297
https://www.dongchedi.com/article/7595668030580179518
https://www.dongchedi.com/article/7595669549916422718
https://www.dongchedi.com/article/7595668022514483737
https://www.dongchedi.com/article/7595667011934732862
https://www.dongchedi.com/article/7595668135898907160
https://www.dongchedi.com/article/7595669519381561880
https://www.dongchedi.com/article/7595665696110314009
https://www.dongchedi.com/article/7595694363196408382
https://www.dongchedi.com/article/7595693923243655704
https://www.dongchedi.com/article/7595695318264578585
https://www.dongchedi.com/article/7595693592938119704
https://www.dongchedi.com/article/7595695213243712024
https://www.dongchedi.com/article/7595694538237346329
https://www.dongchedi.com/article/7595693294714339865
https://www.dongchedi.com/article/7595693592937726488
https://www.dongchedi.com/article/7595693602739618366
https://www.dongchedi.com/article/7595693101252444696

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/27 10:16:21

国家中小学智慧教育平台电子教材获取与解析工具使用指南

国家中小学智慧教育平台电子教材获取与解析工具使用指南 【免费下载链接】tchMaterial-parser 国家中小学智慧教育平台 电子课本下载工具 项目地址: https://gitcode.com/GitHub_Trending/tc/tchMaterial-parser 在数字化教育日益普及的今天,如何高效获取优质…

作者头像 李华
网站建设 2026/3/26 19:59:48

鸣潮自动化工具ok-wuthering-waves:3分钟快速上手指南

鸣潮自动化工具ok-wuthering-waves:3分钟快速上手指南 【免费下载链接】ok-wuthering-waves 鸣潮 后台自动战斗 自动刷声骸上锁合成 自动肉鸽 Automation for Wuthering Waves 项目地址: https://gitcode.com/GitHub_Trending/ok/ok-wuthering-waves ok-wuth…

作者头像 李华
网站建设 2026/3/27 6:48:46

终极指南:3个简单步骤构建你的zotero-style收藏管理体系

终极指南:3个简单步骤构建你的zotero-style收藏管理体系 【免费下载链接】zotero-style zotero-style - 一个 Zotero 插件,提供了一系列功能来增强 Zotero 的用户体验,如阅读进度可视化和标签管理,适合研究人员和学者。 项目地址…

作者头像 李华
网站建设 2026/3/28 11:49:38

免费获取苹果苹方字体:跨平台字体统一终极方案

免费获取苹果苹方字体:跨平台字体统一终极方案 【免费下载链接】PingFangSC PingFangSC字体包文件、苹果平方字体文件,包含ttf和woff2格式 项目地址: https://gitcode.com/gh_mirrors/pi/PingFangSC 还在为Windows和Mac设备上的字体显示差异而困扰…

作者头像 李华
网站建设 2026/3/31 11:32:31

Zotero Style插件终极使用指南:轻松打造高效文献管理神器

Zotero Style插件终极使用指南:轻松打造高效文献管理神器 【免费下载链接】zotero-style zotero-style - 一个 Zotero 插件,提供了一系列功能来增强 Zotero 的用户体验,如阅读进度可视化和标签管理,适合研究人员和学者。 项目地…

作者头像 李华