求助。。。。关于unix 动态链接库coredump的问题

superniyue 2010-06-19 11:31:44
小弟在unix下写动态链接库,仿照网上很多例子来写的,但是到最后总是出现coredump, 希望各位高手们帮小弟解决下这个问题。

/*****************explicit.c ******************/
#include<stdio.h>
#include<dlfcn.h>
int main(int argc,char* argv[])
{
int a = 10;
int b = 20;
int sum = 0;
int(*pSum)();
printf("a=[%d],b=[%d],sum=[%d]\n",a,b,sum);

void *pdlHandle;
char* pdlErr;
printf("open so......\n");

pdlHandle = dlopen("./lib/libtest.so",RTLD_LAZY);

if(!pdlHandle)
{
printf("open dynamic library failed\n");

}
pdlErr = dlerror();
if(pdlErr != NULL)
{
printf("%s\n",pdlErr);
return 0;
}
printf("getting from so....\n");
pSum = (int(*)())dlsym(pdlHandle,"retResult");
pdlErr = dlerror();
if(pdlErr != NULL)
{
printf("%sn", pdlErr);
dlclose(pdlHandle);
return 0;
}
printf("got it!\n");
sum = pSum(a,b);
printf("calling success.........\n");
printf("The sum of a and b is ==== [%d]\n",sum);
dlclose(pdlHandle);
return 0;
/*********************************************************************/


函数指针可能会有点问题,但是绝对不是真正的问题所在,因为我尝试过void类型的,也会coredump。
retResult函数原型很简单,就是输出一句话,返回一个a+b。

编译库的make_lib:

all:libtest.so
CC=cc -q64 -qcpluscmt -g
.c.o:
$(CC) -c -Kpic $<
build_command:
$(CC) -G $(OBJS) -o ./lib/$(DLL)
all_objs=test.o
libtest.so:$(all_objs)
make -f ./make_lib OBJS="$(all_objs)" DLL=$@ build_command

操作系统:AIX

错误信息如下:

a=[10],b=[20],sum=[0]
open so......
getting from so....
got it!
Illegal instruction(coredump);

在线等答复~~~~~也可以加我qq 251157654, 谢谢了!

...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tony2251 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wqkjj 的回复:]
all:libtest.so
CC=cc -q64 -qcpluscmt -g

编译问题。 AIX目前不支持64位的动态库。用32位编译吧。

另外获取动态函数指针的方式,应该改为楼上各位指明的为好。
[/Quote]
ok
wqkjj 2010-06-23
  • 打赏
  • 举报
回复
all:libtest.so
CC=cc -q64 -qcpluscmt -g

编译问题。 AIX目前不支持64位的动态库。用32位编译吧。

另外获取动态函数指针的方式,应该改为楼上各位指明的为好。
superniyue 2010-06-23
  • 打赏
  • 举报
回复
那种改法没有效的,我试过。。
steptodream 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ljhnew 的回复:]
建议用typedef int (*FUNC_SUM)(int,int);

FUNC_SUM pSum;//声明变量,替换int(*pSum)();
sum=psum(a,b);
[/Quote]
顶这个 这样不容易犯楼主这样的错误
ljhnew 2010-06-20
  • 打赏
  • 举报
回复
建议用typedef int (*FUNC_SUM)(int,int);

FUNC_SUM pSum;//声明变量,替换int(*pSum)();
sum=psum(a,b);
mymtom 2010-06-19
  • 打赏
  • 举报
回复
int (*pSum)();
==>
int (*pSum)(int, int);
太乙 2010-06-19
  • 打赏
  • 举报
回复
pSum = (int(*)())dlsym(pdlHandle,"retResult");
==》
pSum = (int(*)(int,int))dlsym(pdlHandle,"retResult");

23,125

社区成员

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

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