关于dlopen的问题

DoDoMouse 2014-02-10 11:07:18
我弄了一个简单的测试程序,想试试linux下调用so文件,程序很简单:
在eclipse下新建2个工程,一个工程输出so包,编译的时候也加了-ldl,要输出的函数(就是一个print函数,打印一些信息)外面也包了extern "C".
编译出来的包通过nm和ldd查看均完好。
另外一个工程调用该包里面的print函数,我使用了dlopen去打开那个so文件,发现返回的handle始终是0.不管采用绝对路径也好,还是项目属性里面指定library path,都不行。

我网上搜了一下,发现把so包放到/usr/lib下居然就可以了,不知道为什么。

请教整个事情的真相,谢谢!
...全文
582 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
DoDoMouse 2014-02-25
  • 打赏
  • 举报
回复
今天再次试验了一把绝对路径,发现也已经可以了,奇怪了.... 先结贴了。
DoDoMouse 2014-02-25
  • 打赏
  • 举报
回复
感谢大家,程序已经跑通了,研究了一把ubuntu下面的link。 这里面通过配置ld.so.config解决了。 试过上面提到的配置LD_LIBRARY_PATH,发现不行,不知道为啥,一直报找不到so文件。
__cc__ 2014-02-20
  • 打赏
  • 举报
回复
说白了,你dlopen用的是相对路径,没找到so文件,所以返回出错。要么设置动态库搜索路径,要么使用绝对路径dlopen
谭海燕 2014-02-20
  • 打赏
  • 举报
回复
引用 10 楼 u011761982 的回复:
返回的handle是0啊,这个地方就已经出错了...
引用 9 楼 Idle_Cloud 的回复:
你dlopen完了,不是还要去搜索函数吗,把那个代码贴出来,以及检查是否错误,以及错误信息。
啥问题?现在还没通么?不行就贴代码吧
Carl_CCC 2014-02-13
  • 打赏
  • 举报
回复
你dlopen完了,不是还要去搜索函数吗,把那个代码贴出来,以及检查是否错误,以及错误信息。
鹤仙人 2014-02-13
  • 打赏
  • 举报
回复
在终端下设置LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dodomouse/workspace/MyLinuxTest/ 再运行一下试试。
Carl_CCC 2014-02-13
  • 打赏
  • 举报
回复
你找个系统的库绝对路径试试,实在不行你就多贴点代码出来吧。
DoDoMouse 2014-02-13
  • 打赏
  • 举报
回复
返回的handle是0啊,这个地方就已经出错了...
引用 9 楼 Idle_Cloud 的回复:
你dlopen完了,不是还要去搜索函数吗,把那个代码贴出来,以及检查是否错误,以及错误信息。
DoDoMouse 2014-02-12
  • 打赏
  • 举报
回复
这个跟你一样的,有何用意?
引用 6 楼 linwhwylb 的回复:
man dlopen 看看你的Linux系统上dlopen的具体实现。 dlopen() The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename con‐ tains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details): o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched. o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon- separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user- ID and set-group-ID programs.) o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched. o The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for file‐ name. o The directories /lib and /usr/lib are searched (in that order).
Linux-Torvalds 2014-02-12
  • 打赏
  • 举报
回复
man dlopen 看看你的Linux系统上dlopen的具体实现。 dlopen() The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename con‐ tains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details): o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched. o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon- separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user- ID and set-group-ID programs.) o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched. o The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for file‐ name. o The directories /lib and /usr/lib are searched (in that order).
DoDoMouse 2014-02-12
  • 打赏
  • 举报
回复
引用 4 楼 zgglj 的回复:
用dlerror得到错误信息,贴出来
没有dlerror信息,输出为空,很奇怪
Carl_CCC 2014-02-11
  • 打赏
  • 举报
回复
我做了个测试,绝对路径没问题,楼主要不把你的程序贴出来看看。
非凡glj 2014-02-11
  • 打赏
  • 举报
回复
用dlerror得到错误信息,贴出来
tempzbj 2014-02-11
  • 打赏
  • 举报
回复
你用strace跟踪一下
DoDoMouse 2014-02-11
  • 打赏
  • 举报
回复
dlopen的调用是这样的:

void* handle = dlopen("/home/dodomouse/workspace/MyLinuxTest/libMyLinuxTest.so", RTLD_LAZY);

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧