LinuxSir.cn,穿越时空的Linuxsir!

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

如何或许调用的动态库的路径?

[复制链接]
发表于 2004-7-16 17:21:21 | 显示全部楼层 |阅读模式
类似于vc中的getModulePath();
因为程序要调用自己动态库a,以及一些别的资源.另外一个动态库需要调用
动态库a.所以动态库a必须获取自己的路径后在修改得到资源的相对路径.我也不知道说明白了没有?
希望大家帮忙!
发表于 2004-7-16 18:22:52 | 显示全部楼层
原来在一个项目中也遇到这个问题,没有办法解决,后来用了别的方法绕过去了。
据说是没有GetModulePath()这样的API
发表于 2004-7-16 19:05:35 | 显示全部楼层
动态库的路径是由系统配置文件指定的。这个配置文件就是/etc/ld.so.conf。看看它就知道了,不需要用什么API来获取。
发表于 2004-7-16 20:41:14 | 显示全部楼层
最初由 kj501 发表
动态库的路径是由系统配置文件指定的。这个配置文件就是/etc/ld.so.conf。看看它就知道了,不需要用什么API来获取。

有些.so文件是通过dlopen动态载入的,所以还是有可能.so不知道自己的路径。
发表于 2004-7-17 09:34:26 | 显示全部楼层
看看manpage是如何说的:
dlopen loads a dynamic library from the file named by the  null  termi-
       nated  string  filename  and returns an opaque "handle" for the dynamic
       library.  If filename is not an absolute path (i.e., it does not  begin
       with a "/"), then the file is searched for in the following locations:

              A   colon-separated   list   of   directories   in   the  user's
              LD_LIBRARY_PATH environment variable.

              The list of libraries cached in /etc/ld.so.cache.

              /lib, followed by /usr/lib.

       If filename is a NULL pointer, then the returned handle is for the main  program.
如果是你自己用绝对路径打开动态库,你自己当然知道。如果不是这样,按照/etc/ld.so.conf中指定的路径,然后是/lib, /usr/lib的顺序搜索就能找到。不过编程时可能麻烦了一些。
发表于 2004-7-17 13:43:36 | 显示全部楼层
要调用自己的动态库,可以由#define指定或者在软件自己的配置文件中指定。
发表于 2004-7-17 14:40:57 | 显示全部楼层
例如我有一个组件库libcom.so放在/usr/local/app/下,它导出了一个函数DllRegisterSever(),用来把自己的一些信息包括路径写入注册表。另外有个注册工具regsvr32打开指定的动态库调用DllRegisterServer()函数,我可能会在home目录下注册这个组件:/home/svenwang$regsvr32 /usr/local/app/libcom.so,我当然希望libcom.so能够知道自己的所在路径。
如果显式地告诉libcom.so所在的位置,不是不可以,但是很麻烦。
发表于 2004-7-18 07:42:50 | 显示全部楼层
楼上的是学VC的吧,linux下那里有注册表呀?
发表于 2004-7-18 21:04:20 | 显示全部楼层
最初由 kj501 发表
楼上的是学VC的吧,linux下那里有注册表呀?

我在linux下自己写的
 楼主| 发表于 2004-7-19 14:11:09 | 显示全部楼层
谢谢各位,我用绝对路径好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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