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

自己实现的第一个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代码<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"></embed>
  1. <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<iostream>
  2. #include<stdlib.h>
  3. #include<dlfcn.h>
  4. usingnamespacestd;
  5. #defineTRUE1
  6. #defineFALSE0
  7. typedefintBOOL;
  8. typedefvoid(*PFUN_STRING)(char*pszStr);
  9. 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<<"TheOriginString:"<<szMsg<<endl;
  28. //opendll
  29. hDll=dlopen("./strcase.dll",RTLD_LAZY);
  30. szDllErr=dlerror();
  31. if(szDllErr)
  32. {
  33. cout<<"openuppercase.dllerror!errinfo:"<<szDllErr<<endl;
  34. returnFALSE;
  35. }
  36. //findthefunction
  37. pfunUpper=(PFUN_STRING)dlsym(hDll,"StrUpper");
  38. szDllErr=dlerror();
  39. if(szDllErr)
  40. {
  41. cout<<"findfunctionStrUpperError!errinfo:"<<szDllErr<<endl;
  42. returnFALSE;
  43. }
  44. (*pfunUpper)(szMsg);
  45. cout<<"afterStrUpperstring:"<<szMsg<<endl;
  46. //callStrLower
  47. pfunLower=(PFUN_STRING)dlsym(hDll,"StrLower");
  48. szDllErr=dlerror();
  49. if(szDllErr)
  50. {
  51. cout<<"findfunctionStrLowerError!errinfo:"<<szDllErr<<endl;
  52. returnFALSE;
  53. }
  54. (*pfunUpper)(szMsg);
  55. cout<<"afterStrLowerstring:"<<szMsg<<endl;
  56. //closehandle
  57. intnRet=dlclose(hDll);
  58. szDllErr=dlerror();
  59. cout<<"closedllinfo:"<<szDllErr<<endl;
  60. }
  61. </span></span>

//lowcase.cpp

Cpp代码<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"></embed>
  1. <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. usingnamespacestd;
  5. extern"C"voidStrLower(char*pszStr)
  6. {
  7. if(NULL==pszStr)
  8. {
  9. return;
  10. }
  11. intnLen=0;
  12. inti=0;
  13. nLen=strlen(pszStr);
  14. for(i=0;i<nLen;i++)
  15. {
  16. if((pszStr[i]>='A')&&(pszStr[i]<='Z'))
  17. {
  18. pszStr[i]+=26;
  19. }
  20. }
  21. }
  22. </span></span>

//uppercase.cpp

Cpp代码<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"></embed>
  1. <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. usingnamespacestd;
  5. extern"C"voidStrUpper(char*pszStr)
  6. {
  7. if(NULL==pszStr)
  8. {
  9. return;
  10. }
  11. intnLen=0;
  12. inti=0;
  13. nLen=strlen(pszStr);
  14. for(i=0;i<nLen;i++)
  15. {
  16. if((pszStr[i]>='a')&&(pszStr[i]<='z'))
  17. {
  18. pszStr[i]-=26;
  19. }
  20. }
  21. }</span>
  22. </span>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值