Linux下C/C++调用静态库、动态库(含隐式调用和显式调用)及调用例子

xingyun86 2020-1-10 1276

Linux下C/C++调用静态库、动态库(含隐式调用和显式调用)及调用例子(文章底部)

概念
动态库和静态库二者的不同点在于代码被载入的时刻不同。

静态库的代码在编译过程中已经被载入可执行程序,因此体积比较大。

动态库(共享库)的代码在可执行程序运行时才载入内存,在编译过程中仅简单的引用,因此代码体积比较小。

静态库链接情况,把库直接加载到程序中。

动态库链接情况,只是保留接口,将动态库与程序代码独立,这样就可以提高代码的可复用度,和降低程序的耦合度。

静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库。

动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在。


示例文件:

test.c:

#include<stdio.h>
#include"test.h"

void test()
{
    printf("welcome to world!\n");
}

test.h:

#ifndef __LIB_H
#define __LIB_H

void test();

#endif

静态库

生成静态库文件:

build_static.sh

#!/bin/sh
gcc -Wall -O2 -fPIC -I./  -c -o test.o test.c
ar crv libtest.a test.o

ar命令的参数如下:

参数     意义
-r      将objfile文件插入静态库尾或者替换静态库中同名文件
-x      从静态库文件中抽取文件objfile
-t      打印静态库的成员文件列表
-d      从静态库中删除文件objfile
-s      重置静态库文件索引
-v      创建文件冗余信息
-c      创建静态库文件

静态库调用示例

test-static.c:

#include<stdio.h>

int main(void)
{
    test();
    return 0;
}

编译脚本:

build_static_call.sh

#!/bin/sh
gcc test-static.c -o test-static ./libtest.a

运行示例:

$ ./test-static 
$ welcome to world!

动态库

生成动态库文件:

build_shared.sh

#!/bin/sh
gcc -o2 -fPIC -shared test.c -o libtest.so

其中:
- fPIC : 产生与位置无关代码,全部使用相对地址.
- shared : 生成动态库.

编译时加载(隐式调用)

动态库调用示例:

test-shared-hidecall.c

#include<stdio.h>

int main()
{
    test();
    return 0;
}

和静态库一样,测试代码不需要包含导出函数的头文件.

编译脚本:

build_shared_hidecall.sh

#!/bin/sh
 gcc -o2 -Wall -L. -ltest test-shared-hidecall.c -o test-shared-hidecall

查看test-shared-hidecall动态段信息,发现已经依赖libtest.so:

$ ldd test-shared-hidecall
    linux-vdso.so.1 =>  (0x00007f0902951000)
    libtest.so => ./libtest.so (0x00007f090274f000)
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003548600000)
    libm.so.6 => /lib64/libm.so.6 (0x000000353de00000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003548200000)
    libc.so.6 => /lib64/libc.so.6 (0x000000353da00000)
    /lib64/ld-linux-x86-64.so.2 (0x000000353d600000)

若此时直接运行,会提示找不到动态库:

$ ./test-shared-hidecall
$ ./test-shared-hidecall: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

可以通过下列四种方法解决:(第四种方法最为简单方便)

# 方法一 修改环境变量
$ export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH

# 方法二 将库文件链接到系统目录下
$ ln -s ./libtest.so /usr/lib

# 方法三 修改/etc/ld.so.conf
$ sudo echo $(pwd) >> /etc/ld.so.conf
$ sudo ldconfig

# 方法四 运行时添加环境变量(此种方法最为简单和方便)
$ LD_LIBRARY_PATH=. ./test-shared-hidecall

再次运行:

$ LD_LIBRARY_PATH=. ./test-shared-hidecall 
$ welcome to world!

运行时链接(显式)

test-shared-showcall.c:

#include<stdio.h>
#include<dlfcn.h>

#define LIB_FILE    "./libtest.so"
#define LIB_TEST    "test"
typedef void (*pfn_test)();

int main(void)
{
    /*
     * RTLD_NOW:将共享库中的所有函数加载到内存 
     * RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数
     */

    void *dl = dlopen(LIB,RTLD_LAZY); //打开动态库

    if (dl == NULL)
    {
        fprintf(stderr,"Error:failed to load libary.\n");
        return -1;
    }
    char *error = dlerror(); //检测错误
    if (error != NULL)
    {
        fprintf(stderr,"%s\n",error);
        dlclose(dl); //关闭动态库
        return -1;
    }

    pfn_test test = (pfn_test)dlsym(dl,LIB_TEST); // 获取函数地址
    if(test == NULL)
    {
        fprintf(stderr,"%s\n",error);
        dlclose(dl); //关闭动态库
        return -1;
    }
    error = dlerror(); //检测错误
    if (error != NULL)
    {
        fprintf(stderr,"%s\n",error);
        dlclose(dl); //关闭动态库
        return -1;
    }

    test(); //调用动态库中的函数

    dlclose(dl); //关闭动态库
    error = dlerror(); //检测错误
    if (error != NULL)
    {
        fprintf(stderr,"%s\n",error);
        return -1;
    }

    return 0;
}

编译脚本:

build_shared_showcall.sh

#!/bin/sh
gcc  -ldl test-shared-showcall.c -o test-shared-showcall

查看test-shared-showcall动态段信息,没有发现依赖libtest.so:

$ ldd test-shared-showcall
    linux-vdso.so.1 =>  (0x00007ffed89e5000)
    libdl.so.2 => /lib64/libdl.so.2 (0x000000353e600000)
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003548600000)
    libm.so.6 => /lib64/libm.so.6 (0x000000353de00000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003548200000)
    libc.so.6 => /lib64/libc.so.6 (0x000000353da00000)
    /lib64/ld-linux-x86-64.so.2 (0x000000353d600000)

运行:

$ ./test-shared-showcall
$ welcome to world!


上传的附件:
×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回