谈一下动态库的构建

电子说

1.2w人已加入

描述

在前面介绍了静态库的构建,这里谈一下动态库的构建,同样以案例的方式描述,希望案例可起到模板的作用。

首先新建一个测试文件,test.c

#include 


int main()
{
  printf("hello world\\n");


  return 0;
}

它是可以编译成功的

Linux系统

将可执行文件a.out删除

Linux系统

添加文件,如静态库中所示(这里省略掉了)

Linux系统

编译运行,同样可以输出

$ gcc test.c func.c
:~/Documents/clan/test2$ tree
.
├── a.out
├── func.c
├── func.h
└── test.c


0 directories, 4 files
:~/Documents/clan/test2$ ./a.out 
hello world
30

到此说明,所有文件是正常的,程序是正常的。

下面清除a.out文件,用动态库的方式实现。

: ~/Documents/clan/test2$ gcc test.c func.c

:~ /Documents/clan/test2$ tree

.

├── a.out

├── func.c

├── func.h

└── test.c

0 directories, 4 files

: ~/Documents/clan/test2$ ./a.out

hello world

30

:~ /Documents/clan/test2$ rm a.out

:~/Documents/clan/test2$ tree

.

├── func.c

├── func.h

└── test.c

0 directories, 3 files

: ~/Documents/clan/test2$ gcc -fPIC -shared -o libfunc.so func.c

:~ /Documents/clan/test2$ tree

.

├── func.c

├── func.h

├── libfunc.so

└── test.c

0 directories, 4 files

: ~/Documents/clan/test2$ gcc test.c libfunc.so

:~ /Documents/clan/test2$ tree

.

├── a.out

├── func.c

├── func.h

├── libfunc.so

└── test.c

0 directories, 5 files

:~/Documents/clan/test2$ ./a.out

./a.out: error while loading shared libraries: libfunc.so: cannot open shared o

Linux系统

`

很可惜,最后在链接动态库编译的时候,失败了,提示不能找到文件

error while loading shared libraries: libfunc.so: cannot open shared object file: No such file or directory

在编译程序时,使用动态链接库和静态库是一致的,使用”-l库名”的方式,在生成可执行文件的时候会链接库文件。

-L指定动态链接库的路径,-ldtiger链接库函数tiger。-ltiger是动态库的调用规则。Linux系统下的动态库命名方式是lib*.so,而在链接时表示位-l*,*是自己命名的库名。

:~/Documents/clan/test2$ rm a.out 
:~/Documents/clan/test2$ gcc test.c -L./ -lfunc
:~/Documents/clan/test2$ tree
.
├── a.out
├── func.c
├── func.h
├── libfunc.so
└── test.c


0 directories, 5 files
:~/Documents/clan/test2$ ./a.out 
./a.out: error while loading shared libraries: libfunc.so: cannot open shared object file: No such file or directory

错误是因为程序运行时没有找到动态链接库造成的。程序编译时链接动态库和运行时使用动态链接库的概念是不同的,在运行时,程序链接的动态链接库需要在系统目录下才行。为此,需要设置临时环境变量

:~/Documents/clan/test2$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
:~/Documents/clan/test2$ gcc test.c -L./ -lfunc
:~/Documents/clan/test2$ ./a.out 
hello world
30

其中export LD_LIBRARY_PATH=$LD_LIBRARY_PATH**:**.表示动态库所在位置为当前目录,将当前目录添加到环境变量中,当终端关闭,这个临时变量就会消失。

Linux系统

上面的运行结果显示,可以正确输出结果,也就是说,动态库是可以成功链接的。下面将上述案例稍微变复杂一点,如果是多个层级目录,又怎么处理,理解路径是非常重要的。

:~/Documents/clan/test2$ tree
.
├── func
│ ├── func.c
│ └── func.h
└── test.c

1 directory, 3 files
:/Documents/clan/test2$ gcc -fPIC -shared -o func/libfunc.so func/func.c
:
/Documents/clan/test2 export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/func
:/Documents/clan/test2$ gcc test.c -L./func -lfunc
:
/Documents/clan/test2$ tree
.
├── a.out
├── func
│ ├── func.c
│ ├── func.h
│ └── libfunc.so
└── test.c

1 directory, 5 files
:~/Documents/clan/test2$ ./a.out
hello world
30

Linux系统

下面我们将用Makefile的形式来使用动态库,编辑Makefile文件

export LD_LIBRARY_PATH:=$LD_LIBRARY_PATH:/func

test : test.c func/libfunc.so

gcc -o test test.c -L./func -lfunc

func/libfunc.so : func/func.c func/func.h

gcc -fPIC -shared -o func/libfunc.so func/func.c

clean :

rm -f test func/libfunc.so

Linux系统

然后编译执行,同样可以输出正确结果

Linux系统

动态库的过程比静态库稍微复杂,但是在写Makefile的时候,步骤基本相似,倒着写gcc编译过程即可。

审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分