自己实现的第一个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>
- <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<iostream>
- #include<stdlib.h>
- #include<dlfcn.h>
- usingnamespacestd;
- #defineTRUE1
- #defineFALSE0
- typedefintBOOL;
- typedefvoid(*PFUN_STRING)(char*pszStr);
- BOOLUseDll(char*szMsg);
- intmain()
- {
- charszMsg[]="Hello,andylin!";
- UseDll(szMsg);
- return0;
- }
- BOOLUseDll(char*szMsg)
- {
- void*hDll=NULL;
- char*szDllErr=NULL;
- PFUN_STRINGpfunUpper=NULL;
- PFUN_STRINGpfunLower=NULL;
- if(NULL==szMsg)
- {
- returnFALSE;
- }
- cout<<"TheOriginString:"<<szMsg<<endl;
- hDll=dlopen("./strcase.dll",RTLD_LAZY);
- szDllErr=dlerror();
- if(szDllErr)
- {
- cout<<"openuppercase.dllerror!errinfo:"<<szDllErr<<endl;
- returnFALSE;
- }
- pfunUpper=(PFUN_STRING)dlsym(hDll,"StrUpper");
- szDllErr=dlerror();
- if(szDllErr)
- {
- cout<<"findfunctionStrUpperError!errinfo:"<<szDllErr<<endl;
- returnFALSE;
- }
- (*pfunUpper)(szMsg);
- cout<<"afterStrUpperstring:"<<szMsg<<endl;
- pfunLower=(PFUN_STRING)dlsym(hDll,"StrLower");
- szDllErr=dlerror();
- if(szDllErr)
- {
- cout<<"findfunctionStrLowerError!errinfo:"<<szDllErr<<endl;
- returnFALSE;
- }
- (*pfunUpper)(szMsg);
- cout<<"afterStrLowerstring:"<<szMsg<<endl;
- intnRet=dlclose(hDll);
- szDllErr=dlerror();
- cout<<"closedllinfo:"<<szDllErr<<endl;
- }
- </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>
- <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<stdio.h>
- #include<stdlib.h>
- #include<iostream>
- usingnamespacestd;
- extern"C"voidStrLower(char*pszStr)
- {
- if(NULL==pszStr)
- {
- return;
- }
- intnLen=0;
- inti=0;
- nLen=strlen(pszStr);
- for(i=0;i<nLen;i++)
- {
- if((pszStr[i]>='A')&&(pszStr[i]<='Z'))
- {
- pszStr[i]+=26;
- }
- }
- }
- </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>
- <spanstyle="font-size:medium;"><spanstyle="color:#3366ff;">#include<stdio.h>
- #include<stdlib.h>
- #include<iostream>
- usingnamespacestd;
- extern"C"voidStrUpper(char*pszStr)
- {
- if(NULL==pszStr)
- {
- return;
- }
- intnLen=0;
- inti=0;
- nLen=strlen(pszStr);
- for(i=0;i<nLen;i++)
- {
- if((pszStr[i]>='a')&&(pszStr[i]<='z'))
- {
- pszStr[i]-=26;
- }
- }
- }</span>
- </span>