- 论坛徽章:
- 0
|
我在做一个动态库测试中老是不成功:
==== 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
请高人指点,向同志们学习!!! |
|