---
title: "自己实现的第一个linux中dll的调用"
author: "congfeng02"
date: "2009-07-08T16:59:00Z"
tags: []
slug: "linux-dll"
draft: false
lastmod: "2009-07-08T16:59:00Z"
id: 435177
url: "https://congfeng02.iteye.com/blog/435177"
---

### [自己实现的第一个linux中dll的调用](http://andylin02.iteye.com/blog/422015)

[编辑](http://andylin02.iteye.com/admin/blogs/422015/edit)

自己实现的第一个linux中dll的调用

1\. 编译dll:

g++ -shared -lc -o strcase.dll lowcase.cpp uppercase.cpp

如果要加入调试信息:(加上 -g选项)

g++ -shared -lc -o strcase.dll lowcase.cpp uppercase.cpp -g

编译c++ dll注意函数之前需要加上 extern "C",否则在dlsym函数调用的时候可能会找不到函数

.

2\. 编译exe:

g++ -ldl -o strcase.exe main.cpp

如果要加入调试信息:(加上 -g选项)

g++ -ldl -o strcase.exe main.cpp -g

3\. gdb调试

gdb strcase.exe

调试时需要在编译的时候加上(-g)选项

gdb命令:

b 设置断点,例如:

b main #表示跳到main函数

b main.cpp: 56 #56表示行号

l 打印当前的执行代码附近的10行

p 打印变量szMsg的值

p szMsg

n 执行下一行(单步执行, 类似VC中的F10)

s 进入函数(类似VC中的F10)

r 执行(类似VC中的F5)

//main.cpp

Cpp代码&lt;embed type="application/x-shockwave-flash" width="14" height="15" src="http://andylin02.iteye.com/javascripts/syntaxhighlighter/clipboard\_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%3Cspan%20style%3D%22font-size%3A%20medium%3B%22%3E%3Cspan%20style%3D%22color%3A%20%233366ff%3B%22%3E%23include%20%3Ciostream%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Cdlfcn.h%3E%0Ausing%20namespace%20std%3B%0A%0A%23define%09TRUE%091%0A%23define%20FALSE%090%0Atypedef%20int%20%09BOOL%3B%0Atypedef%20void%20(\*PFUN\_STRING)(char\*%20pszStr)%3B%0A%0ABOOL%20UseDll(char\*%20szMsg)%3B%0A%0Aint%20main()%0A%7B%0A%09char%20szMsg%5B%5D%20%3D%20%22Hello%2C%20andylin!%22%3B%0A%09%0A%09%2F%2F%E8%B0%83%E7%94%A8dll%0A%09UseDll(szMsg)%3B%0A%09%0A%09return%200%3B%0A%7D%0A%0ABOOL%20UseDll(char\*%20szMsg)%0A%7B%0A%09void\*%20hDll%20%3D%20NULL%3B%0A%09char\*%20szDllErr%20%3D%20NULL%3B%0A%09PFUN\_STRING%20pfunUpper%20%3D%20NULL%3B%0A%09PFUN\_STRING%20pfunLower%20%3D%20NULL%3B%0A%09%0A%09if%20(NULL%20%3D%3D%20szMsg)%0A%09%7B%0A%09%09return%20FALSE%3B%0A%09%7D%0A%09%0A%09cout%20%3C%3C%20%22The%20Origin%20String%3A%22%20%3C%3C%20szMsg%20%3C%3C%20endl%3B%0A%09%0A%09%2F%2Fopen%20dll%0A%09hDll%20%3D%20dlopen(%22.%2Fstrcase.dll%22%2C%20RTLD\_LAZY)%3B%0A%09szDllErr%20%3D%20dlerror()%3B%09%0A%09if%20(szDllErr)%0A%09%7B%0A%09%09cout%20%3C%3C%20%22open%20uppercase.dll%20error!%20err%20info%3A%22%20%3C%3C%20szDllErr%20%3C%3C%20endl%3B%0A%09%09return%20FALSE%3B%0A%09%7D%0A%09%0A%09%2F%2Ffind%20the%20function%0A%09pfunUpper%20%3D%20(PFUN\_STRING)dlsym(hDll%2C%20%22StrUpper%22)%3B%09%0A%09szDllErr%20%3D%20dlerror()%3B%0A%09%0A%09if%20(szDllErr)%0A%09%7B%0A%09%09cout%20%3C%3C%20%22find%20function%20StrUpper%20Error!%20err%20info%3A%22%20%3C%3C%20szDllErr%20%3C%3C%20endl%3B%0A%09%09return%20FALSE%3B%0A%09%7D%0A%09%0A%09(\*pfunUpper)(szMsg)%3B%0A%09cout%20%3C%3C%20%22after%20StrUpper%20string%3A%22%20%3C%3C%20szMsg%20%3C%3C%20endl%3B%0A%09%0A%09%2F%2Fcall%20StrLower%0A%09pfunLower%20%3D%20(PFUN\_STRING)dlsym(hDll%2C%20%22StrLower%22)%3B%0A%09szDllErr%20%3D%20dlerror()%3B%0A%09%0A%09if%20(szDllErr)%0A%09%7B%0A%09%09cout%20%3C%3C%20%22find%20function%20StrLower%20Error!%20err%20info%3A%22%20%3C%3C%20szDllErr%20%3C%3C%20endl%3B%0A%09%09return%20FALSE%3B%0A%09%7D%0A%09%0A%09(\*pfunUpper)(szMsg)%3B%0A%09cout%20%3C%3C%20%22after%20StrLower%20string%3A%22%20%3C%3C%20szMsg%20%3C%3C%20endl%3B%0A%09%0A%09%2F%2Fclose%20handle%0A%09int%20nRet%20%3D%20dlclose(hDll)%3B%0A%09szDllErr%20%3D%20dlerror()%3B%0A%09cout%20%3C%3C%20%22close%20dll%20info%3A%22%20%3C%3C%20szDllErr%20%3C%3C%20endl%3B%09%0A%7D%0A%3C%2Fspan%3E%3C%2Fspan%3E"&gt;&lt;/embed&gt;

01. &lt;spanstyle="font-size:medium;"&gt;&lt;spanstyle="color:#3366ff;"&gt;#include&lt;iostream&gt;
02. #include&lt;stdlib.h&gt;
03. #include&lt;dlfcn.h&gt;
04. usingnamespacestd;
05. #defineTRUE1
06. #defineFALSE0
07. typedefintBOOL;
08. typedefvoid(\*PFUN\_STRING)(char\*pszStr);
09. BOOLUseDll(char\*szMsg);
10. intmain()
11. {
12. charszMsg\[]="Hello,andylin!";
13. //调用dll
14. UseDll(szMsg);
15. return0;
16. }
17. BOOLUseDll(char\*szMsg)
18. {
19. void\*hDll=NULL;
20. char\*szDllErr=NULL;
21. PFUN\_STRINGpfunUpper=NULL;
22. PFUN\_STRINGpfunLower=NULL;
23. if(NULL==szMsg)
24. {
25. returnFALSE;
26. }
27. cout&lt;&lt;"TheOriginString:"&lt;&lt;szMsg&lt;&lt;endl;
28. //opendll
29. hDll=dlopen("./strcase.dll",RTLD\_LAZY);
30. szDllErr=dlerror();
31. if(szDllErr)
32. {
33. cout&lt;&lt;"openuppercase.dllerror!errinfo:"&lt;&lt;szDllErr&lt;&lt;endl;
34. returnFALSE;
35. }
36. //findthefunction
37. pfunUpper=(PFUN\_STRING)dlsym(hDll,"StrUpper");
38. szDllErr=dlerror();
39. if(szDllErr)
40. {
41. cout&lt;&lt;"findfunctionStrUpperError!errinfo:"&lt;&lt;szDllErr&lt;&lt;endl;
42. returnFALSE;
43. }
44. (\*pfunUpper)(szMsg);
45. cout&lt;&lt;"afterStrUpperstring:"&lt;&lt;szMsg&lt;&lt;endl;
46. //callStrLower
47. pfunLower=(PFUN\_STRING)dlsym(hDll,"StrLower");
48. szDllErr=dlerror();
49. if(szDllErr)
50. {
51. cout&lt;&lt;"findfunctionStrLowerError!errinfo:"&lt;&lt;szDllErr&lt;&lt;endl;
52. returnFALSE;
53. }
54. (\*pfunUpper)(szMsg);
55. cout&lt;&lt;"afterStrLowerstring:"&lt;&lt;szMsg&lt;&lt;endl;
56. //closehandle
57. intnRet=dlclose(hDll);
58. szDllErr=dlerror();
59. cout&lt;&lt;"closedllinfo:"&lt;&lt;szDllErr&lt;&lt;endl;
60. }
61. &lt;/span&gt;&lt;/span&gt;

//lowcase.cpp

Cpp代码&lt;embed type="application/x-shockwave-flash" width="14" height="15" src="http://andylin02.iteye.com/javascripts/syntaxhighlighter/clipboard\_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%3Cspan%20style%3D%22font-size%3A%20medium%3B%22%3E%3Cspan%20style%3D%22color%3A%20%233366ff%3B%22%3E%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%0Aextern%20%22C%22%20void%20StrLower(char\*%20pszStr)%0A%7B%0A%09if%20(NULL%20%3D%3D%20pszStr)%0A%09%7B%0A%09%09return%3B%0A%09%7D%0A%0A%09int%20nLen%20%3D%200%3B%0A%09int%09i%20%3D%200%3B%0A%09%0A%09nLen%20%3D%20strlen(pszStr)%3B%0A%09for%20(i%20%3D%200%3B%20i%20%3C%20nLen%3B%20i%2B%2B)%0A%09%7B%0A%09%09if%20(%20(pszStr%5Bi%5D%20%3E%3D%20'A')%20%26%26%20(pszStr%5Bi%5D%20%3C%3D%20'Z')%20)%0A%09%09%7B%0A%09%09%09pszStr%5Bi%5D%20%2B%3D%2026%3B%0A%09%09%7D%0A%09%7D%0A%7D%0A%3C%2Fspan%3E%3C%2Fspan%3E"&gt;&lt;/embed&gt;

01. &lt;spanstyle="font-size:medium;"&gt;&lt;spanstyle="color:#3366ff;"&gt;#include&lt;stdio.h&gt;
02. #include&lt;stdlib.h&gt;
03. #include&lt;iostream&gt;
04. usingnamespacestd;
05. extern"C"voidStrLower(char\*pszStr)
06. {
07. if(NULL==pszStr)
08. {
09. return;
10. }
11. intnLen=0;
12. inti=0;
13. nLen=strlen(pszStr);
14. for(i=0;i&lt;nLen;i++)
15. {
16. if((pszStr\[i]&gt;='A')&&(pszStr\[i]&lt;='Z'))
17. {
18. pszStr\[i]+=26;
19. }
20. }
21. }
22. &lt;/span&gt;&lt;/span&gt;

//uppercase.cpp

Cpp代码&lt;embed type="application/x-shockwave-flash" width="14" height="15" src="http://andylin02.iteye.com/javascripts/syntaxhighlighter/clipboard\_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%3Cspan%20style%3D%22font-size%3A%20medium%3B%22%3E%3Cspan%20style%3D%22color%3A%20%233366ff%3B%22%3E%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%0Aextern%20%22C%22%20void%20StrUpper(char\*%20pszStr)%0A%7B%0A%09if%20(NULL%20%3D%3D%20pszStr)%0A%09%7B%0A%09%09return%3B%0A%09%7D%0A%0A%09int%20nLen%20%3D%200%3B%0A%09int%09i%20%3D%200%3B%0A%09%0A%09nLen%20%3D%20strlen(pszStr)%3B%0A%09for%20(i%20%3D%200%3B%20i%20%3C%20nLen%3B%20i%2B%2B)%0A%09%7B%0A%09%09if%20(%20(pszStr%5Bi%5D%20%3E%3D%20'a')%20%26%26%20(pszStr%5Bi%5D%20%3C%3D%20'z')%20)%0A%09%09%7B%0A%09%09%09pszStr%5Bi%5D%20-%3D%2026%3B%0A%09%09%7D%0A%09%7D%0A%7D%3C%2Fspan%3E%0A%3C%2Fspan%3E"&gt;&lt;/embed&gt;

01. &lt;spanstyle="font-size:medium;"&gt;&lt;spanstyle="color:#3366ff;"&gt;#include&lt;stdio.h&gt;
02. #include&lt;stdlib.h&gt;
03. #include&lt;iostream&gt;
04. usingnamespacestd;
05. extern"C"voidStrUpper(char\*pszStr)
06. {
07. if(NULL==pszStr)
08. {
09. return;
10. }
11. intnLen=0;
12. inti=0;
13. nLen=strlen(pszStr);
14. for(i=0;i&lt;nLen;i++)
15. {
16. if((pszStr\[i]&gt;='a')&&(pszStr\[i]&lt;='z'))
17. {
18. pszStr\[i]-=26;
19. }
20. }
21. }&lt;/span&gt;
22. &lt;/span&gt;