首先有必要介绍一下静态库和动态库的区别。简单的说静态库就是在生成可执行文件的时候把用到的函数代码加入到其中。而动态库只是在运行的时候才会被加载。
动态库的应用有两个主要原因:
1. 共享。多个程序可以使用同一个动态库
2. 开发模式好。要求设计者对功能划分得更好
但动态库装载的时候慢,特别是运行中装载有明显延迟。
静态库的优点:
代码装载速度快,执行速度略比动态库快,没有什么更多的优势。
下面介绍一下在linux下实用gcc创建共享库的方法。
构建共享库最基本的一步就是编译库中的对象模块。还是通过例子来说明吧。两个源文件如下:
使用下列命令可将这两个源文件编译成一个目标文件:$ gcc -c -fpic shellofirst.c shellosecond.c
选项-fpic使得输出的对象模块是按照可重定位地址方式生成的。缩写pic代表位置独立代码(position independent code)。
下列命令使用目标文件构造共享库hello.so
$ gcc -shared shellofirst.o shellosecond.o -o hello.so
共享库hello.so就构造完毕了。下面编写主程序测试共享库的使用。
使用下列命令将该程序编译并连接到共享库:
一旦程序被连接,为了使用共享库,就必须在运行的时候能够找到共享库的位置 。gcc载入程序并运行的时候,共享库应该位于一下位置:
1. 环境变量LD_LIBRARY_PATH列出的所有用分号分割的目录
2.文件/etc/ld.so.cache中找到的库的列表,由工具ldconfig维护。
3.目录/usr/lib
4.目录/usr/local/lib
所以为了运行我们的程序,必须使用上面列出的四种方法中的一种使程序可以定位到共享库。
我们使用命令ldd 查看程序使用了哪些库,命令及执行结果如下:
从中可以看到我们自己创建的共享库hello.so没有找到,现在试图运行程序会产生如下错误:
错误已经很明显了,就是我们的程序无法定位共享库。在这里我们使用第一种方法解决这个问题,输入如下命令:
在共享库的搜索路径中加入当前目录,然后运行程序即可。
动态库的应用有两个主要原因:
1. 共享。多个程序可以使用同一个动态库
2. 开发模式好。要求设计者对功能划分得更好
但动态库装载的时候慢,特别是运行中装载有明显延迟。
静态库的优点:
代码装载速度快,执行速度略比动态库快,没有什么更多的优势。
下面介绍一下在linux下实用gcc创建共享库的方法。
构建共享库最基本的一步就是编译库中的对象模块。还是通过例子来说明吧。两个源文件如下:
1
/* shellofirst.c */
2 #include < stdio.h >
3 void shellofirst()
4 {
5 printf("The first hello from a shared library ");
6 }
2 #include < stdio.h >
3 void shellofirst()
4 {
5 printf("The first hello from a shared library ");
6 }
1
/* shellosecond.c */
2 #include < stdio.h >
3 void shellosecond()
4 {
5 printf("The second hello from a shared library ");
6 }
2 #include < stdio.h >
3 void shellosecond()
4 {
5 printf("The second hello from a shared library ");
6 }
使用下列命令可将这两个源文件编译成一个目标文件:$ gcc -c -fpic shellofirst.c shellosecond.c
选项-fpic使得输出的对象模块是按照可重定位地址方式生成的。缩写pic代表位置独立代码(position independent code)。
下列命令使用目标文件构造共享库hello.so
$ gcc -shared shellofirst.o shellosecond.o -o hello.so
共享库hello.so就构造完毕了。下面编写主程序测试共享库的使用。
1
/* stwohellos.c */
2 void shellofirst( void );
3 void shellosecond( void );
4 int main( int argc, char * argv[])
5 {
6 shellofirst();
7 shellosecond();
8 return(0);
9 }
~
2 void shellofirst( void );
3 void shellosecond( void );
4 int main( int argc, char * argv[])
5 {
6 shellofirst();
7 shellosecond();
8 return(0);
9 }
~
使用下列命令将该程序编译并连接到共享库:
$ gcc stwohellos.c hello.so
-
o stwohellos
一旦程序被连接,为了使用共享库,就必须在运行的时候能够找到共享库的位置 。gcc载入程序并运行的时候,共享库应该位于一下位置:
1. 环境变量LD_LIBRARY_PATH列出的所有用分号分割的目录
2.文件/etc/ld.so.cache中找到的库的列表,由工具ldconfig维护。
3.目录/usr/lib
4.目录/usr/local/lib
所以为了运行我们的程序,必须使用上面列出的四种方法中的一种使程序可以定位到共享库。
我们使用命令ldd 查看程序使用了哪些库,命令及执行结果如下:
$ ldd stwohellos
linux - gate.so. 1 => ( 0x00fdd000 )
hello.so => not found
libc.so. 6 => / lib / libc.so. 6 ( 0x49856000 )
/ lib / ld - linux.so. 2 ( 0x48e85000 )
linux - gate.so. 1 => ( 0x00fdd000 )
hello.so => not found
libc.so. 6 => / lib / libc.so. 6 ( 0x49856000 )
/ lib / ld - linux.so. 2 ( 0x48e85000 )
从中可以看到我们自己创建的共享库hello.so没有找到,现在试图运行程序会产生如下错误:
.
/
stwohellos: error
while
loading shared libraries: hello.so: cannot open shared
object
file: No such file or directory
错误已经很明显了,就是我们的程序无法定位共享库。在这里我们使用第一种方法解决这个问题,输入如下命令:
$ export LD_LIBRARY_PATH
=
$LD_LIBRARY_PATH:.
/
在共享库的搜索路径中加入当前目录,然后运行程序即可。
$ .
/
stwohellos
The first hello from a shared library
The second hello from a shared library
The first hello from a shared library
The second hello from a shared library