免费注册 查看新帖 |

Chinaunix

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

在Digital UNIX 下如何能正确使用动态库,急急急!!! [复制链接]

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

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

  3. int prt( char * s )
  4. {
  5.     cout << " this is in libtest.so !!! " << endl;
  6.     cout << s << endl;
  7.     return 0;
  8. }
复制代码

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

  1. #include <iostream.h>;
  2. #include <stdio.h>;
  3. #include <dlfcn.h>;

  4. //int upTo( char * );

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

  11.    cout << " ++++ enter in main ... ++++ " << endl;
  12.    cout << " ***.input a string " << endl;
  13.    cin >;>; str;
  14.    // open the *.so(*.dll) file
  15.    if ( ( handle = dlopen( "libtest.so" , RTLD_LAZY ) ) == NULL )
  16.    {
  17.       cout << " ***.error in open libtest.so " << endl;
  18.       return 0;
  19.    }
  20.    // get the function in libtest.so
  21.    if ( ( prt = ( int (*)(char*) )dlsym( handle , "prt" ) )== NULL )
  22.    {
  23.       cout << " ***.error in get function " << endl;
  24.       erro_info = dlerror( );
  25.       cout << " ***.the error information : " << erro_info << endl;
  26.       return 0;
  27.    }
  28.    // call the function in libtest.so
  29.    prt( str );
  30.    cout << str << endl;
  31.    // close the libtest.so
  32.    dlclose( handle );
  33.    return 1;
  34. }
复制代码

================================
编译连接主程序:
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) 出错的地方是在:

  1.   // get the function in libtest.so
  2.    if ( ( prt = ( int (*)(char*) )dlsym( handle , "prt" ) )== NULL )
  3.    {
  4.       cout << " ***.error in get function " << endl;
  5.       erro_info = dlerror( );
  6.       cout << " ***.the error information : " << erro_info << endl;
  7.       return 0;
  8.    }
复制代码

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

请高人指点,向同志们学习!!!
jmsyoung 该用户已被删除
2 [报告]
发表于 2004-07-21 14:51 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP