Linux的静态库和动态库

静态库

静态库命名规则

静态库文件命名为libxxx.a

lib为固定前缀

xxx为库的名字,库名字内容长度自己定。注意库的名字和库文件名字是两个东西

.a为固定的库文件后缀

静态库的制作与使用

静态库代码的编写

//math.h
class CMath
{
public:
    CMath();
    ~CMath();

    static int add(int a, int b);

    static int sub(int a, int b);

    static int mul(int a, int b);

    static int div(int a, int b);

    void print();

};

将代码文件编译成目标文件.o

g++ -c math.cpp

注意带参数-c,否则直接编译为可执行文件

通过ar工具将目标文件打包成.a静态库文件

ar -crv libmath.a matho

生成静态库libmath.a

使用静态库

//test.cpp
#include "math.h"
#include <iostream>

using namespace std;

int main()
{
    cout << CMath::add(1, 2) << endl;
    cout << CMath::sub(5, 2) << endl;
    cout << CMath::mul(5, 5) << endl;
    cout << CMath::div(10, 2) << endl;

    CMath math;
    math.print();

    return 0;
}

Linux下使用静态库,只需要在编译的时候,指定静态库的搜索路径(-L选项)、指定静态库名(不需要lib前缀和.a后缀,-l选项)

g++ test.cpp -L./ -lmath -o test

-L:表示要连接的库所在目录

-l:指定链接时需要的动态库,编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.a或.so来确定库的名称。

动态库

linux动态库的命名规则

动态链接库的名字形式为 libxxx.so,前缀是lib,后缀名为“.so”

  • 针对于实际库文件,每个共享库都有个特殊的名字“soname”。在程序启动后,程序通过这个名字来告诉动态加载器该载入哪个共享库。

  • 在文件系统中,soname仅是一个链接到实际动态库的链接。对于动态库而言,每个库实际上都有另一个名字给编译器来用。它是一个指向实际库镜像文件的链接文件(lib+soname+.so)。

动态库的代码编写
//math.h
class CMath
{
public:
    CMath();
    ~CMath();

    static int add(int a, int b);

    static int sub(int a, int b);

    static int mul(int a, int b);

    static int div(int a, int b);

    void print();

};
创建动态库

生成目标文件,此时要加编译器选项-fpic

g++ -fPIC -c math.cpp

-fPIC 创建与地址无关的编译程序(pic,position independent code),是为了能够在多个应用程序间共享

生成动态库,此时要加链接器选项-shared

g++ -shared -o limath.so math.o

其实上面两个步骤可以合并为一个命令:

g++ -fPIC -shared -o libmath.so math.cpp
使用动态库
//test.cpp
#include "math.h"
#include <iostream>

using namespace std;

int main()
{
    cout << CMath::add(1, 2) << endl;
    cout << CMath::sub(5, 2) << endl;
    cout << CMath::mul(5, 5) << endl;
    cout << CMath::div(10, 2) << endl;

    CMath math;
    math.print();

    return 0;
}
g++ test.cpp -L./ -lmath -o test

运行

sh-4.2# ./test 
./test: error while loading shared libraries: libmath.so: cannot open shared object file: No such file or directory

在执行的时候是如何定位共享库文件的呢?

当系统加载可执行代码时候,能够知道其所依赖的库的名字,但是还需要知道绝对路径。此时就需要系统动态载入器(dynamic linker/loader)。

对于elf格式的可执行程序,是由ld-linux.so*来完成的,它先后搜索elf文件的 DT_RPATH段—环境变量LD_LIBRARY_PATH—/etc/ld.so.cache文件列表—/lib/,/usr/lib 目录找到库文件后将其载入内存。

如何让系统能够找到它:

l 如果安装在/lib或者/usr/lib下,那么ld默认能够找到,无需其他操作。

l 如果安装在其他目录,需要将其添加到/etc/ld.so.cache文件中,步骤如下:

n 编辑/etc/ld.so.conf文件,加入库文件所在目录的路径

n 运行ldconfig ,该命令会重建/etc/ld.so.cache文件

我们将创建的动态库复制到/usr/lib下面,然后运行测试程序

在Linux下显式调用动态库

#include <dlfcn.h>,提供了下面几个接口:

  • void * dlopen( const char * pathname, int mode ):函数以指定模式打开指定的动态连接库文件,并返回一个句柄给调用进程。

  • void* dlsym(void* handle,const char* symbol):dlsym根据动态链接库操作句柄(pHandle)与符号(symbol),返回符号对应的地址。使用这个函数不但可以获取函数地址,也可以获取变量地址。

  • int dlclose (void *handle):dlclose用于关闭指定句柄的动态链接库,只有当此动态链接库的使用计数为0时,才会真正被系统卸载。

  • const char *dlerror(void):当动态链接库操作函数执行失败时,dlerror可以返回出错信息,返回值为NULL时表示操作函数执行成功。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值