免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 28980 | 回复: 13

dlopen,dlsym的问题,实在搞不明白了。 [复制链接]

论坛徽章:
0
发表于 2006-09-11 14:51 |显示全部楼层
学习使用dlopen等函数动态加载函数,测试程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int usage()
{
return 1;
}

int main()
{
void* handle,*p;
char * error;
handle = dlopen(NULL,RTLD_NOW);
if (!handle) {
    fputs (dlerror(), stderr);
    exit(1);
}
p = dlsym(handle, "usage");
if ((error = dlerror()) != NULL)  {
     fputs(error, stderr);
     exit(1);
}
   
}
运行后,dlsym的返回值是0,会打印出undefined symbol usage,是什么原因阿?是我的代码写的有问题吗?

论坛徽章:
0
发表于 2006-09-11 14:59 |显示全部楼层
dlsym返回的是NULL,表示调用不成功。
dlxxx系列函数不是这样用的,一般都是打开一个动态库,然后映射动态库的函数

论坛徽章:
0
发表于 2006-09-11 23:14 |显示全部楼层
原帖由 sunlan 于 2006-9-11 14:59 发表
dlsym返回的是NULL,表示调用不成功。


非也非也
man dlsym

论坛徽章:
0
发表于 2006-09-11 23:35 |显示全部楼层
原帖由 JohnBull 于 2006-9-11 23:14 发表


非也非也
man dlsym


If handle does not refer to a valid object opened by dlopen(), or if the named symbol cannot be found within any of the objects associated with handle, dlsym() shall return NULL. More detailed diagnostic information shall be available through dlerror().

楼主说dlsym返回的是0,也就是return NULL

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2006-09-12 16:03 |显示全部楼层
sunlan 摘录的是哪儿的 man 手册?
Linux 的确不是这样的。
下面是我的 Debian 上的 man page:
  1.    dlsym
  2.        The  function dlsym() takes a "handle" of a dynamic library returned by
  3.        dlopen() and the null-terminated symbol  name,  returning  the  address
  4.        where  that  symbol is loaded into memory.  If the symbol is not found,
  5.        in the specified library or any of the libraries  that  were  automati-
  6.        cally  loaded by dlopen() when that library was loaded, dlsym() returns
  7.        NULL.  (The search performed by dlsym() is breadth  first  through  the
  8.        dependency  tree  of  these  libraries.)  Since the value of the symbol
  9.        could actually be NULL (so that a NULL return  from  dlsym()  need  not
  10.        indicate  an  error),  the  correct way to test for an error is to call
  11.        dlerror() to clear any old error conditions,  then  call  dlsym(),  and
  12.        then call dlerror() again, saving its return value into a variable, and
  13.        check whether this saved value is not NULL.
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2006-09-12 16:06 |显示全部楼层
                                                 Since the value of the symbol
       could actually be NULL (so that a NULL return  from  dlsym()  need  not
       indicate  an  error),  the  correct way to test for an error is to call
       dlerror() to clear any old error conditions,  then  call  dlsym(),  and
       then call dlerror() again, saving its return value into a variable, and
       check whether this saved value is not NULL.

论坛徽章:
0
发表于 2007-01-03 11:53 |显示全部楼层
把你的useage.so加入到LD_LIBRARY_PATH中, 而且同时要把它链接入你的main中

论坛徽章:
0
发表于 2007-01-03 12:08 |显示全部楼层
NetBSD

man dlsym

     dlsym() looks for a definition of symbol in the shared object designated
     by handle. The symbols address is returned. If the symbol cannot be re-
     solved, NULL is returned.

论坛徽章:
0
发表于 2007-01-03 12:45 |显示全部楼层
关键这里 usage 的位置不可能是 NULL(不需要动态连接的符号),所以应该还是存在问题的。

楼主用的 g++ 编译的?

论坛徽章:
0
发表于 2007-01-03 12:57 |显示全部楼层
dlsym()是将库中的一个函数绑定到预定义的函数地址,所以不能定义p是void *的。看关于uage()的定义,可定义函数指针为:
...
int (*usg)(void);
...
*(void **) (&usg)=dlsym(handle,"usage");
...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP