AIX平台下使用共享库的示例

本文演示了在AIX平台上如何使用共享库。通过两个示例文件mytest.cpp和mytestso.cpp,详细介绍了编译及运行过程,并强调了使用extern C声明函数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

演示如何在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"来声明,调用时将会报错说函数未实现。

转载于:https://www.cnblogs.com/allanyan/archive/2008/02/27/1083795.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值