【Linux系统】静态库与动态库

库制作与原理

1. 什么是库

库是写好的现有的,成熟的,可以复用的代码。现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常。

本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。库有两种:

  • 静态库.a[Linux]、.lib[windows]
  • 动态库.so[Linux]、.dll[windows]
// ubuntu 动静态库
$ ls -l /lib/x86_64-linux-gnu/libc-2.31.so
-rwxr-xr-x 1 root root 2029592 May  1 02:20 /lib/x86_64-linux-gnu/libc-2.31.so
$ ls -l /lib/x86_64-linux-gnu/libc.a
-rw-r--r-- 1 root root 5747594 May  1 02:20 /lib/x86_64-linux-gnu/libc.a

// C++
$ ls /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.so -l
lrwxrwxrwx 1 root root 40 Oct 24 2022 /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.so -> ../../../../x86_64-linux-gnu/libstdc++.so.6
$ ls /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a

// Centos 动静态库
// C
$ ls -l /lib64/libc-2.17.so
-rwxr-xr-x 1 root root 2156592 Jun  4 23:05 /lib64/libc-2.17.so
[whb@bite-alicloud ~]$ ls /lib64/libc.a -l
-rw-r--r-- 1 root root 5105516 Jun  4 23:05 /lib64/libc.a

// C++
$ ls /lib64/libstdc++.so.6 -l
lrwxrwxrwx 1 root root 19 Sep 18 20:59 /lib64/libstdc++.so.6 -> libstdc++.so.6.0.19
$ ls -l /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.a -l
-rw-r--r-- 1 root root 2932366 Sep 30 2020 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.a

预备工作

// my_stdio.h
#pragma once
#define SIZE 1024
#define FLUSH_NONE 0
#define FLUSH_LINE 1
#define FLUSH_FULL 2

struct IO_FILE
{
    int flag; // 刷新方式
    int fileno; // 文件描述符
    char outbuffer[SIZE];
    char cap;
    int size;
    // TODO
};
typedef struct IO_FILE mFILE;

mFILE *mfopen(const char *filename, const char *mode);
int mfwrite(const void *ptr, int num, mFILE *stream);
void mfflush(mFILE *stream);
void mfclose(mFILE *stream);
// my_stdio.c
#include "my_stdio.h"
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

mFILE *mfopen(const char *filename, const char *mode)
{
    int fd = -1;
    if(strcmp(mode, "r") == 0)
    {
        fd = open(filename, O_RDONLY);
    }
    else if(strcmp(mode, "w")== 0)
    {
        fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0666);
    }
    else if(strcmp(mode, "a") == 0)
    {
        fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 0666);
    }
    if(fd < 0) return NULL;
    mFILE *mf = (mFILE*)malloc(sizeof(mFILE));
    if(!mf)
    {
        close(fd);
        return NULL;
    }
    mf->fileno = fd;
    mf->flag = FLUSH_LINE;
    mf->size = 0;
    mf->cap = SIZE;
    return mf;
}

void mfflush(mFILE *stream)
{
    if(stream->size > 0)
    {
        // 写到内核文件的文件缓冲区中!
        write(stream->fileno, stream->outbuffer, stream->size);
        // 刷新到外设
        fsync(stream->fileno);
        stream->size = 0;
    }
}

int mfwrite(const void *ptr, int num, mFILE *stream)
{
    // 1. 拷贝
    memcpy(stream->outbuffer+stream->size, ptr, num);
    stream->size += num;
    // 2. 检测是否要刷新
    if(stream->flag == FLUSH_LINE && stream->size > 0 && stream->outbuffer[stream->size-1]== '\n')
    {
        mfflush(stream);
    }
    return num;
}

void mfclose(mFILE *stream)
{
    if(stream->size > 0)
    {
        mfflush(stream);
    }
    close(stream->fileno);
}
// my_string.h
#pragma once
int my_strlen(const char *s);
// my_string.c
#include "my_string.h"
int my_strlen(const char *s)
{
    const char *end = s;
    while(*end != '\0')end++;
    return (end - s);
}

2. 静态库

  • 静态库(.a):程序在编译链接的时候把库的代码链接到可执行文件中,程序运行的时候将不再需要静态库。
  • 一个可执行程序可能用到许多的库,这些库运行有的是静态库,有的是动态库,而我们的编译默认为动态链接库,只有在该库下找不到动态.so的时候才会采用同名静态库。我们也可以使用gcc的 -static 强转设置链接静态库。

2-1静态库生成

// Makefile
libmystdio.a:my_stdio.o my_string.o
        @ar -rc $@ $^
        @echo "build $^ to $@ ... done"
%.o:%.c
        @gcc -c $<
        @echo "compiling $< to $@ ... done"

.PHONY:clean
clean:
        @rm -rf *.a *.o stdc*
        @echo "clean ... done"

.PHONY:output
output:
        @mkdir -p stdc/include
        @mkdir -p stdc/lib
        @cp -f *.h stdc/include
        @cp -f *.a stdc/lib
        @tar -czf stdc.tgz stdc
        @echo "output stdc ... done"
  • ar 是gnu归档工具,rc表示(replace and create)
$ ar -tv libmystdio.a
rw-rw-r-- 1000/1000  2848 Oct 29 14:35 2024 my_stdio.o
rw-rw-r-- 1000/1000  1272 Oct 29 14:35 2024 my_string.o
  • t:列出静态库中的文件
  • v:verbose 详细信息

2-2静态库使用

// 任意目录下,新建
// main.c,引入库头文件
#include "my_stdio.h"
#include "my_string.h"
#include <stdio.h>

int main()
{
    const char *s = "abcdefg";
    printf("%s: %d\n", s, my_strlen(s));

    mFILE *fp = mfopen("./log.txt", "a");
    if(fp == NULL) return 1;

    mfwrite(s, my_strlen(s), fp);
    mfwrite(s, my_strlen(s), fp);
    mfwrite(s, my_strlen(s), fp);

    mfclose(fp);
    return 0;
}
  • // 场景1: 头文件和库文件安装到系统路径下
$ gcc main.c -lmystdio
  • // 场景2: 头文件和库文件和我们自己的源文件在同一个路径下
$ gcc main.c -L. -lmymath
  • // 场景3: 头文件和库文件有自己的独立路径
$ gcc main.c -I头文件路径 -L库文件路径 -lmymath
  • -L:指定库路径
  • -I:指定头文件搜索路径
  • -l:指定库名
  • 测试目标文件生成后,静态库删掉,程序照样可以运行
  • 关于 -static 选项,稍后介绍
  • 库文件名称和引入库的名称:去掉前缀lib,去掉后缀.so,.a,如:libc.so -> c

3. 动态库

  • 动态库(.so):程序在运行的时候才去链接动态库的代码,多个程序共享使用库的代码。
  • 一个与动态库链接的可执行文件仅仅包含它用到的函数入口地址的一个表,而不是外部函数所在目标文件的整个机器码。
  • 在可执行文件开始运行以前,外部函数的机器码由操作系统从磁盘上的该动态库中复制到内存中,这个过程称为动态链接(dynamic linking)。
  • 动态库可以在多个程序间共享,所以动态链接使得可执行文件更小,节省了磁盘空间。操作系统采用虚拟内存机制允许物理内存中的一份动态库被要用到该库的所有进程共用,节省了内存和磁盘空间

3-1动态库生成

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

.PHONY:clean
clean:
    @rm -rf *.so *.o stdc*
    @echo "clean ... done"

.PHONY:output
output:
    @mkdir -p stdc/include
    @mkdir -p stdc/lib
    @cp -f *.h stdc/include
    @cp -f *.so stdc/lib
    @tar -czf stdc.tgz stdc
    @echo "output stdc ... done"
  • shared: 表示生成共享库格式
  • fPIC: 产生位置无关联码(position independent code)
  • 库名规则: libxxx.so

3-2动态库使用

  • // 场景1: 头文件和库文件安装到系统路径下
$ gcc main.c -lmystdio
  • // 场景2: 头文件和库文件和我们自己的源文件在同一个路径下
$ gcc main.c -L. -lmymath  // 从左到右搜索-L指定的目录
  • // 场景3: 头文件和库文件有自己的独立路径
$ gcc main.c -I头文件路径 -L库文件路径 -lmymath
$ ldd libmystdio.so // 查看库或者可执行程序的依赖
        linux-vdso.so.1 =>  (0x00007fffacbbf000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f8917335000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f8917905000)

// 以场景2为例
$ ll
total 24
-rwxrwxr-x 1 whb whb 8592 Oct 29 14:50 libmystdio.so
-rw-rw-r-- 1 whb whb  359 Oct 19 16:07 main.c
-rw-rw-r-- 1 whb whb  447 Oct 29 14:50 my_stdio.h
-rw-rw-r-- 1 whb whb  447 Oct 29 14:50 my_string.h
$ gcc main.c -L. -lmystdio
$ ll
total 36
-rwxrwxr-x 1 whb whb 8600 Oct 29 14:51 a.out
-rwxrwxr-x 1 whb whb 8592 Oct 29 14:50 libmystdio.so
-rw-rw-r-- 1 whb whb  359 Oct 19 16:07 main.c
-rw-rw-r-- 1 whb whb  447 Oct 29 14:50 my_stdio.h
-rw-rw-r-- 1 whb whb  447 Oct 29 14:50 my_string.h
[whb@bite-alicloud other]$ ./a.out
...

3-3库运行搜索路径

3-3-1问题
$ ldd a.out
        linux-vdso.so.1 =>  (0x00007fff4d396000)
        libmystdio.so => not found
        libc.so.6 => /lib64/libc.so.6 (0x00007fa2aef30000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa2af2fe000)
3-3-2解决方案
  • 拷贝.so文件到系统共享库路径下,一般指/usr/lib/usr/local/lib/lib64或者开篇指明的库路径等。
  • 向系统共享库路径下建立同名软连接。
  • 更改环境变量:LD_LIBRARY_PATH
  • ldconfig方案:配置/etc/ld.so.conf.d/ldconfig更新
[root@localhost linux]# cat /etc/ld.so.conf.d/bit.conf
/root/tools/linux
[root@localhost linux]# ldconfig  // 要生效,这里要执行ldconfig,重新加载库搜索路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值