阅读: 171 评论: 0 作者: allanyan 发表于 2008-02-27 15:13 原文链接
演示如何在AIX平台下使用共享库
OS:AIX Version 5.2 (64bit)
文件关系
mytest.cpp用于生成mytest*,它将调用共享库mytestso.so,mytestso.cpp用于生成mytestso.so*
源文件如下:
1
//
FileName: mytestso.cpp
2
3 #include < stdio.h >
4 #include < stdlib.h >
5
6 extern " C " int GetMax( int a, int b);
7 extern " C " int GetInt( char * psztxt);
8
9 int GetMax( int a, int b)
10 {
11 if(a >= b)
12 return a;
13 else
14 return b;
15}
16 int GetInt( char * psztxt)
17 {
18 if(0 == psztxt)
19 return -1;
20 else
21 return atoi(psztxt);
22}
23
2
3 #include < stdio.h >
4 #include < stdlib.h >
5
6 extern " C " int GetMax( int a, int b);
7 extern " C " int GetInt( char * psztxt);
8
9 int GetMax( int a, int b)
10 {
11 if(a >= b)
12 return a;
13 else
14 return b;
15}
16 int GetInt( char * psztxt)
17 {
18 if(0 == psztxt)
19 return -1;
20 else
21 return atoi(psztxt);
22}
23
用下述指令编译共享库
% g++ -fpic -shared mytestso.cpp -o mytestso.so
1
//
FileName: mytest.cpp
2
3 #include < dlfcn.h >
4 #include < stdio.h >
5 int main( int argc, char * argv[])
6 {
7 void* pdlhandle;
8 char* pszerror;
9
10 int (*GetMax)(int a, int b);
11 int (*GetInt)(char* psztxt);
12
13 int a, b;
14 char* psztxt = "1024";
15
16 // open mytestso.so
17 pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
18 pszerror = dlerror();
19 if (0 != pszerror)
20 {
21 printf("dlopen error: %s\n", pszerror);
22 return 1;
23 }
24
25 // get GetMax func
26 GetMax = (int (*)(int, int))dlsym(pdlhandle, "GetMax");
27 pszerror = dlerror();
28 if (0 != pszerror)
29 {
30 printf("Call GetMax error: %s\n", pszerror);
31 return 1;
32 }
33
34 // get GetInt func
35 GetInt = (int (*)(char*))dlsym(pdlhandle, "GetInt");
36 pszerror = dlerror();
37 if (0 != pszerror)
38 {
39 printf("Call GetInt error: %s\n", pszerror);
40 return 1;
41 }
42
43 // call fun
44 a = 200;
45 b = 600;
46 printf("max=%d\n", GetMax(a, b));
47 printf("txt=%d\n", GetInt(psztxt));
48
49 // close mytestso.so
50 dlclose(pdlhandle);
51}
52
2
3 #include < dlfcn.h >
4 #include < stdio.h >
5 int main( int argc, char * argv[])
6 {
7 void* pdlhandle;
8 char* pszerror;
9
10 int (*GetMax)(int a, int b);
11 int (*GetInt)(char* psztxt);
12
13 int a, b;
14 char* psztxt = "1024";
15
16 // open mytestso.so
17 pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
18 pszerror = dlerror();
19 if (0 != pszerror)
20 {
21 printf("dlopen error: %s\n", pszerror);
22 return 1;
23 }
24
25 // get GetMax func
26 GetMax = (int (*)(int, int))dlsym(pdlhandle, "GetMax");
27 pszerror = dlerror();
28 if (0 != pszerror)
29 {
30 printf("Call GetMax error: %s\n", pszerror);
31 return 1;
32 }
33
34 // get GetInt func
35 GetInt = (int (*)(char*))dlsym(pdlhandle, "GetInt");
36 pszerror = dlerror();
37 if (0 != pszerror)
38 {
39 printf("Call GetInt error: %s\n", pszerror);
40 return 1;
41 }
42
43 // call fun
44 a = 200;
45 b = 600;
46 printf("max=%d\n", GetMax(a, b));
47 printf("txt=%d\n", GetInt(psztxt));
48
49 // close mytestso.so
50 dlclose(pdlhandle);
51}
52
用下述指令编译
% g++ mytest.cpp -ldl -o mytest
运行结果
%mytest
max=600
txt=1024
值得注意的是,在共享库源文件中,必须用 extern "C"来声明共享库中的函数,这样编译器会按照C的格式来编译共享库,否则按C++的方式编译共享库。两者编译后的效果完全不同。可以通过nm命令观察编译后共享库中的符号。
如果没有用 extern "C"来声明:
%nm *.so | grep GetMax
._Z6GetMaxii T 268435936
_Z6GetMaxii D 537011260
_Z6GetMaxii d 537011260 12
如果使用 extern "C"来声明:
nm *.so | grep GetMax
.GetMax T 268435936
GetMax D 537011260
GetMax d 537011260 12
可见,如果不用 extern "C"来声明,调用时将会报错说函数未实现。
最新新闻:
· 金蝶开始进军个人市场,推出个人理财产品。(2010-06-01 11:22)
· 华硕推出平板电脑Eee Pad 最低售价399美元(2010-06-01 11:00)
· 2010软件收入百强企业出炉:华为居首(2010-06-01 10:54)
· 华硕结盟亚马逊在线图书 挖ipad墙角(2010-06-01 10:36)
· 腾讯QQ医生趁乱升级肉搏安全市场(2010-06-01 10:33)
编辑推荐:关于Java与.NET的讨论
网站导航:博客园首页 个人主页 新闻 闪存 小组 博问 社区 知识库