经过一个例子来介绍如何生成一个动态库。假设有一个头文件so_test.h和三个C文件:test_a.c、test_b.c、test_c.c linux
/* so_test.h */ #include <stdio.h> #include <stdlib.h> void test_a(); void test_b(); void test_c();
/* test_a.c */ #include "so_test.h" void test_a(){ printf("this is in test_a...\n"); }
/* test_b.c */ #include "so_test.h" void test_b(){ printf("this is in test_b...\n"); }
/* test_c.c */ #include "so_test.h" void test_c(){ printf("this is in test_c...\n"); }
将这几个文件编译成一个动态库:libtest.so
shell
gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so参数说明:
-shared 该选项指定生成动态链接库(让链接器生成T类型的导出符号表,有时候也生成弱链接W类型的导出符号),不用该标志外部程序没法链接。至关于一个可执行文件
-fPIC 表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的因此动态载入时是经过代码拷贝的方式来知足不一样进程的须要,而不能达到真正代码段共享的目的。
若是在windows下使用MinGW进行编译,则要将上述命令中的libtest.so改为libtest.dll windows
下面编写一个test.c调用刚生成的动态连接库libtest.so: this
/* test.c */ #include "so_test.h" int main(){ test_a(); test_b(); test_c(); return 0; }使用以下的命令将test.c编译成test.o,并和libtest.so连接到一块儿造成 可执行文件test
gcc test.c -L. -ltest -o test参数说明:
-L.:表示要链接的库在当前目录中(也能够设置环境变量LD_LIBRARY_PATH进行指定)
-ltest:编译器查找动态链接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来肯定库的名称 spa
执行./test看可否输出指望的结果: code
this is in test_a... this is in test_b... this is in test_c...
[test@192.168.1.102 ~/workspace/so$]ldd test linux-gate.so.1 => (0x00c5f000) libtest.so (0x00c20000) libc.so.6 => /lib/libc.so.6 (0x00157000) /lib/ld-linux.so.2 (0x00131000)
调用动态库的时候有几个问题会常常碰到,有时,明明已经将库的头文件所在目录 经过 “-I” include进来了,库所在文件经过 “-L”参数引导,并指定了“-l”的库名,但经过ldd命令察看时,就是死活找不到你指定连接的so文件,这时你要做的就是经过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定动态库的目录。一般这样作就能够解决库没法连接的问题了。
进程