动态库so在Linux下用c和c++编程时经常会碰到,下面简单介绍动态库的编译和链接
2、动态库的链接
最主要的是GCC命令行的一个选项:
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号), 不用该标志外部程序无法连接。相当于一个可执行文件
-fPIC 表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
-L.表示要连接的库在当前目录中
-ltest编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名称
4、注意
调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找不到你指定链接的so文件,这时你要作的就是通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定加载动态链接库的路径。通常这样做就可以解决库无法链接的问题了。
然后再输入:export
即会显示是否设置正确
export方式在重启后失效,所以也可以用 vim /etc/bashrc ,修改其中的LD_LIBRARY_PATH变量。
1、动态库so的编译
下面通过一个例子来介绍如何生成一个动态库。
这里有一个头文件:so_test.h三个c文件:test_a.c、test_b.c、test_c.c
我们将这几个文件编译成一个动态库:libtest.so。
<span style="font-family:Courier New;font-size:14px;"><span style="color:#ff0000;">//so_test.h:</span><span style="color:#494949;">
#include "stdio.h"
void test_a();
void test_b();
void test_c();</span></span>
<span style="font-family:Courier New;font-size:14px;"><span style="color:#ff0000;">//test_a.c:</span>
#include "so_test.h"
void test_a()
{
printf("this is in test_a...\n");
}</span>
<span style="font-family:Courier New;font-size:14px;"><strong><span style="color:#ff0000;">//test_b.c:</span><span style="color:#494949;">
#include "so_test.h"
void test_b()
{
printf("this is in test_b...\n");
}</span></strong></span>
<span style="font-family:Courier New;font-size:14px;"><strong><span style="color:#ff0000;">//test_c.c:</span><span style="color:#494949;">
#include "so_test.h"
void test_c()
{
printf("this is in test_c...\n");
}</span></strong></span>
将这几个文件编译成一个动态库:libtest.so
$ gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so2、动态库的链接
在1.中,我们已经成功生成了一个自己的动态链接库libtest.so,下面我们通过一个程序来调用这个库里的函数。
程序的源文件为:test.c。
<span style="font-family:Courier New;font-size:14px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; background-color: rgb(236, 240, 252);"><span style="color:#ff0000;">//test.c:</span></span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);">#include "so_test.h"</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);">int main()</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);">{</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);"> test_a();</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);"> test_b();</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);"> test_c();</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);"> return 0;</span><br style="color: rgb(73, 73, 73); line-height: 21px; background-color: rgb(236, 240, 252);" /><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); background-color: rgb(236, 240, 252);">}</span></span>
将test.c与动态库libtest.so链接生成执行文件test:$ gcc test.c -L. -ltest -o test
$ ldd test
执行test,可以看到它是如何调用动态库中的函数的。
$ ./test
最主要的是GCC命令行的一个选项:
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号), 不用该标志外部程序无法连接。相当于一个可执行文件
-fPIC 表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
-L.表示要连接的库在当前目录中
-ltest编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名称
4、注意
调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找不到你指定链接的so文件,这时你要作的就是通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定加载动态链接库的路径。通常这样做就可以解决库无法链接的问题了。
LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。
当然如果有root权限的话,可以修改/etc/ld.so.conf文件,然后调用 /sbin/ldconfig来达到同样的目的,不过如果没有root权限,那么只能采用输出LD_LIBRARY_PATH的方法了。
在linux下可以用export命令来设置这个值,在linux终端下输入:
export LD_LIBRARY_PATH= /opt/test/bin:$LD_LIBRARY_PATH: ( 注 :/opt/test/bin为你的so文件所在目录)然后再输入:export
即会显示是否设置正确
export方式在重启后失效,所以也可以用 vim /etc/bashrc ,修改其中的LD_LIBRARY_PATH变量。
例如:LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/au1200_rm/build_tools/bin。