人间四月天

          轻轻的我走了,正如我轻轻的来;
                 我轻轻的招手,作别西天的云彩。
   那河畔的金柳,是夕阳中的新娘;
             波光里的艳影,在我的心头荡漾。

演示如何在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
 6extern "C" int GetMax(int a, int b);
 7extern "C" int GetInt(char* psztxt);
 8
 9int GetMax(int a, int b)
10{
11    if(a >= b)
12        return a;
13    else
14        return b;
15}

16int 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>
 5int 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 (*)(intint))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"来声明,调用时将会报错说函数未实现。
posted on 2008-02-27 15:13  allanyan  阅读(1046)  评论(0编辑  收藏  举报

日志宝-在线日志分析平台