- 论坛徽章:
- 0
|
AIX xlC 动态链接库+静态库的问题
c.c文件
#include <stdio.h>;
int
afunc()
{
#if( defined __hp__ || defined HPUX )
return 100;
#else
return 101;
#endif
}
b.c
#include <stdio.h>;
#include <dlfcn.h>;
extern int afunc();
int
bfunc()
{
fprintf( stdout, "in bfunc\n" );
fprintf( stdout, "afunc=%d\n", afunc() );
return 0;
}
a.c
#include <stdlib.h>;
#include <stdio.h>;
#include <dlfcn.h>;
typedef int (*func_portal)(); /*func_portal的定义同SO中的函数类型有关*/
main()
{
void *dlhandle;
void *pfunc;
func_portal MainFunc;
int rc;
dlhandle = dlopen( "./libb.so", RTLD_LAZY );
if( dlhandle == NULL ) {
fprintf( stderr, "dlopen error(%s)\n", dlerror() );
exit( -1 );
}
fprintf( stdout, "dlhandle succ\n" );
pfunc = dlsym( dlhandle, "bfunc" );
if( pfunc == NULL ) {
fprintf( stderr, "dlsym error(%s)\n", dlerror() );
exit( -1 );
}
}
MainFunc=(func_portal)pfunc;
rc=MainFunc();
printf("%d\n",rc);
dlclose( dlhandle );
exit( 0 );
}
编译过程
第一步:生成"点O文件"
命令: cc -c b.c c.c
第二步:生成"点SO文件"
命令: ld -G -lc -bnoentry -bexpall b.o c.o -o libb.so
第三步: 生成调用SO的文件
命令: cc a.c -o a.out
运行环境:AIX4.3.3+c for aix |
|