C/C++ 静态库、动态库的制作与使用

静态库和动态库

文章目录

静态库和动态库

1.1 什么是库

1.2 静态库的制作

1.3 动态库的制作

1.4 静态库/动态库的优缺点

1.1 什么是库

库可以提供给使用者一些需要的变量、函数或类

库文件有两种,静态库和动态库(共享库)

静态库在程序的链接阶段被复制到了程序中

动态库在程序运行时由系统动态加载到内存中被调用

好处:1. 代码保密 2. 方便部署和分发

1.2 静态库的制作

命名:

Linux: libxxx.a

Windows: libxxx.lib

制作
    # 获得 .o 文件
    gcc  *.c  -c
    # 打包 .o 文件
    ar  rcs  libxxx.a  *.o
    # 链接 xxx库
    gcc  *.c  -o  main  -lxxx
    # 执行
    ./main

ar rcs libxxx.a *.o

注:*.o文件,随便一种 IDE都可以获得?

1.3 动态库的制作

命名

Linux: libxxx.so

Windows: libxxx.dll

制作
    # 获得和位置无关的 .o 文件
    gcc  -c  -fpic  *.c 
    # 获得 .so 动态库
    gcc  -shared  *.o -o libxxx.so
    # 链接 XXX库
    gcc  *.c  -o  main  -lxxx
    # 执行前需要指定共享库路径,步骤:
    ## ldd(list dynamic dependencies)查看动态库依赖关系
    ldd main
    ## 将缺少的库加入环境变量 LD_LIBRARY_PATH
    export LD_LIBRARY_PATH = /xxx/path:$LD_LIBRARY_PATH
    # 执行
    ./main

gcc -shared *.o -o libxxx.so

# 获得和位置无关的 .o 文件

gcc -c -fpic *.c

https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html

-fpic

Generate position-independent code (PIC) suitable for use in a shared

library, if supported for the target machine. Such code accesses all

constant addresses through a global offset table (GOT). The dynamic

loader resolves the GOT entries when the program starts (the dynamic

loader is not part of GCC; it is part of the operating system). If

the GOT size for the linked executable exceeds a machine-specific

maximum size, you get an error message from the linker indicating that-fpic does not work; in that case, recompile with -fPICinstead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k

on the m68k and RS/6000. The x86 has no such limit.)

Position-independent code requires special support, and therefore works

only on certain machines. For the x86, GCC supports PIC for System V

but not for the Sun 386i. Code generated for the IBM RS/6000 is always

position-independent.

When this flag is set, the macros __pic__ and __PIC__are defined to 1

如何定位共享库文件呢?

当系统加载可执行代码时候,能够知道其所依赖的库的名字,但是还需要知道绝对路径。此时就需要系统的动态载入器来获取该绝对路径。对于elf格式的可执行程序,是由ld-linux.so来完成的,它先后搜索elf文件的 DT_RPATH段 ——> 环境变量LD_LIBRARY_PATH ——> /etc/ld.so.cache文件列表 ——> /lib/,/usr/lib目录找到库文件后将其载入内存。

1.4 静态库/动态库的优缺点

对比优点缺点

静态库加载速度快;移植方便(无需提供静态库)消耗系统资源(内存);更新、部署、发布麻烦

动态库进程间资源共享;更新、部署、发布简单加载速度慢;移植需提供动态库

当两个程序运行同时需要同一库时,静态库会在内存中加载两次,动态库只加载一次。

————————————————

原文链接:https://blog.csdn.net/liusscsdn/article/details/123642202

参考:

C/C++编译以及静态库和动态库的制作和使用

https://blog.csdn.net/qq_37233070/article/details/120544919?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-120544919-blog-127719762.pc_relevant_3mothn_strategy_and_data_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3

C++编写库文件以及调用

https://blog.csdn.net/qq_41596730/article/details/127719762

C++库的创建和使用

https://blog.csdn.net/weixin_39289876/article/details/123847837

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值