LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 933|回复: 6

请教几个建立动态连接库的问题

[复制链接]
发表于 2004-8-26 00:06:05 | 显示全部楼层 |阅读模式
[zd@localhost test1]$ vi print.c
[zd@localhost test1]$ gcc -fPIC -g -c -Wall print.c
[zd@localhost test1]$ gcc -shared -W1,soname,libmyprint.so.1 -o
libmyprint.so.1.0.1 print.o -lc
[zd@localhost test1]$ ldconfig -n ~/storage/program/test1
[zd@localhost test1]$ LD_LIBRARY_PATH=/home/zd/storage/program/test1
[zd@localhost test1]$ export LD_LIBRARY_PATH
[zd@localhost test1]$ ln -sf libmyprint.so.1.0.1 libmyprint.so.1
[zd@localhost test1]$ ln -sf libmyprint.so.1 libmyprint.so
[zd@localhost test1]$ vi print.h
[zd@localhost test1]$ vi myprint.c
[zd@localhost test1]$ gcc myprint.c -o myprint -L. -lmyprint
请问这里这里ldconfig在这里有什么作用?
还有为什么要建立两个符号连接?不是说是用soname找到相应的函数库的吗?
发表于 2004-8-27 10:00:37 | 显示全部楼层
1.可以看看ldconfig的man page。
2.便于对动态库进行管理,避免库文件升级后对使用它的应用程序造成影响。
 楼主| 发表于 2004-8-27 23:40:14 | 显示全部楼层
可是如果我不建这两个符号连接,好像就不能使用这个动态函数库了
发表于 2004-8-29 00:01:10 | 显示全部楼层
这正是为了避免动态库升级造成的影响啊。。。
一般使用动态库的程序在编译时,比如说要使用libfoo.so,使用的编译参数是在命令行上加一个-lfoo,这样在程序编译时,会自动去/usr/lib或/lib目录(或者在/etc/ld.so.conf中指定的目录)下寻找libfoo.so这个文件。如果没有一个文件名一模一样的文件 ,即使有一个libfoo.so.1.1文件,也会报错。如果让程序直接在编译时链接到libfoo.1.1,则动态库如果升级到libfoo.1.2,则需要重新编译程序,以便程序链接到libfoo.1.2。为了避免这个问题,一般是让程序连接到一个与版本号无关的文件名,如libfoo.so,然后把这个libfoo.so链接到具体的libfoo.so.1.1,将来一旦升级为libfoo.1.2,则重新链接即可,不需要重新编译程序了。
 楼主| 发表于 2004-8-30 23:12:26 | 显示全部楼层
那你刚才说的这个过程里好像没有用到soname.
发表于 2004-8-31 21:32:50 | 显示全部楼层
你没搞清soname是用来做什么的,看看ld的man文档吧:
-soname=name
           When creating an ELF shared  object,  set  the  internal  DT_SONAME
           field  to  the specified name.  When an executable is linked with a
           shared object which has a DT_SONAME field, then when the executable
           is  run  the  dynamic linker will attempt to load the shared object
           specified by the DT_SONAME field rather than  the  using  the  file
           name given to the linker.
 楼主| 发表于 2004-8-31 22:59:09 | 显示全部楼层
也就是说建立符号连接是用于编译是找到函数库,而soname是用于程序运行时通过soname找到连接库.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表