惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - allanyan

修改ORACLE RAC的字符集(记录一下) Red Hat Enterprise Linux的一些简单操作(备忘录) 也说腾讯打算开放API Android 开发日志04——CheckBox与ImageButton Android 开发日志01——Say Hi程序 Android 开发日志02——您最喜欢哪个品牌的汽车 如何用Word 2007写Blog - allanyan - 博客园 男人恋爱绝不做19件事 所谓“没有缘份”仅仅是爱情故事中一个无法处理的异常情况 男人在有了女人之后 婚前婚后 男女间八种没有结果的感情 终于可以用Windows Live Writer 写博客了 苏格拉底与失恋者的对话 人间四月天[转贴] XManager 配置说明 Win32 API下的多线程编程 Linux的一些有用的命令 Windows 下的FTP脚本
AIX平台下使用共享库的示例
allanyan · 2008-02-27 · via 博客园 - allanyan

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