免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1900 | 回复: 0
打印 上一主题 下一主题

[SCO UNIX] 有关动态库问题,请高人指点!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-03 09:34 |只看该作者 |倒序浏览
我在做一个动态库测试中老是不成功:
==== lib_test.cpp========
代码:

#include <iostream.h>;
#include <stdio.h>;

int prt( char * s )
{
    cout << " this is in libtest.so !!! " << endl;
    cout << s << endl;
    return 0;
}


==================
cxx -fpic -c lib_test.cpp
cxx -shared -o libtest.so lib_test.o
生成动态库:libtest.so
其中: 系统提示,fpic 被忽略
==== test.cpp ==========
代码:

#include <iostream.h>;
#include <stdio.h>;
#include <dlfcn.h>;

//int upTo( char * );

int main( int argc , char *argv[] )
{
   void * handle ; // the handle of the lib.so
   char * erro_info = NULL;
   int (*prt)( char* );// the function pointer
   char str[80] = "";

   cout << " ++++ enter in main ... ++++ " << endl;
   cout << " ***.input a string " << endl;
   cin >;>; str;
   // open the *.so(*.dll) file
   if ( ( handle = dlopen( "libtest.so" , RTLD_LAZY ) ) == NULL )
   {
      cout << " ***.error in open libtest.so " << endl;
      return 0;
   }
   // get the function in libtest.so
   if ( ( prt = ( int (*)(char*) )dlsym( handle , "prt" ) )== NULL )
   {
      cout << " ***.error in get function " << endl;
      erro_info = dlerror( );
      cout << " ***.the error information : " << erro_info << endl;
      return 0;
   }
   // call the function in libtest.so
   prt( str );
   cout << str << endl;
   // close the libtest.so
   dlclose( handle );
   return 1;
}


================================
编译连接主程序:
cxx test.cpp -o test -I/usr/include -I/usr/myapp -L/usr/ccs/lib -L/usr/myapp -lm -lcurses -ldl
现在问题有两个:
1。如果加上 -ldl 系统会连接不过,报错:
ld (prelink):
Can't locate file for: -ldl
ld:
Can't locate file for: -ldl
*** Exit 1
2。如果去掉 -ldl 连接通过,生成可执行文件 test 但是运行时不能调用动态库中的函数prt( char * s) 出错的地方是在:
代码:

  // get the function in libtest.so
   if ( ( prt = ( int (*)(char*) )dlsym( handle , "prt" ) )== NULL )
   {
      cout << " ***.error in get function " << endl;
      erro_info = dlerror( );
      cout << " ***.the error information : " << erro_info << endl;
      return 0;
   }


错误信息:
***.error in get function
***.the error information : dlsym: Unable to locate symbol prt

请高人指点,向同志们学习!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP