现在的位置: 首页 > 综合 > 正文

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

2013年11月11日 ⁄ 综合 ⁄ 共 3294字 ⁄ 字号 评论关闭

 

自己实现的第一个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代码 
  1. <span style="font-size: medium;"><span style="color: #3366ff;">#include <iostream>  
  2. #include <stdlib.h>  
  3. #include <dlfcn.h>  
  4. using namespace std;  
  5.   
  6. #define TRUE    1  
  7. #define FALSE   0  
  8. typedef int     BOOL;  
  9. typedef void (*PFUN_STRING)(char* pszStr);  
  10.   
  11. BOOL UseDll(char* szMsg);  
  12.   
  13. int main()  
  14. {  
  15.     char szMsg[] = "Hello, andylin!";  
  16.       
  17.     //调用dll  
  18.     UseDll(szMsg);  
  19.       
  20.     return 0;  
  21. }  
  22.   
  23. BOOL UseDll(char* szMsg)  
  24. {  
  25.     void* hDll = NULL;  
  26.     char* szDllErr = NULL;  
  27.     PFUN_STRING pfunUpper = NULL;  
  28.     PFUN_STRING pfunLower = NULL;  
  29.       
  30.     if (NULL == szMsg)  
  31.     {  
  32.         return FALSE;  
  33.     }  
  34.       
  35.     cout << "The Origin String:" << szMsg << endl;  
  36.       
  37.     //open dll  
  38.     hDll = dlopen("./strcase.dll", RTLD_LAZY);  
  39.     szDllErr = dlerror();     
  40.     if (szDllErr)  
  41.     {  
  42.         cout << "open uppercase.dll error! err info:" << szDllErr << endl;  
  43.         return FALSE;  
  44.     }  
  45.       
  46.     //find the function  
  47.     pfunUpper = (PFUN_STRING)dlsym(hDll, "StrUpper");     
  48.     szDllErr = dlerror();  
  49.       
  50.     if (szDllErr)  
  51.     {  
  52.         cout << "find function StrUpper Error! err info:" << szDllErr << endl;  
  53.         return FALSE;  
  54.     }  
  55.       
  56.     (*pfunUpper)(szMsg);  
  57.     cout << "after StrUpper string:" << szMsg << endl;  
  58.       
  59.     //call StrLower  
  60.     pfunLower = (PFUN_STRING)dlsym(hDll, "StrLower");  
  61.     szDllErr = dlerror();  
  62.       
  63.     if (szDllErr)  
  64.     {  
  65.         cout << "find function StrLower Error! err info:" << szDllErr << endl;  
  66.         return FALSE;  
  67.     }  
  68.       
  69.     (*pfunUpper)(szMsg);  
  70.     cout << "after StrLower string:" << szMsg << endl;  
  71.       
  72.     //close handle  
  73.     int nRet = dlclose(hDll);  
  74.     szDllErr = dlerror();  
  75.     cout << "close dll info:" << szDllErr << endl;      
  76. }  
  77. </span></span>  

 

 

//lowcase.cpp

 

Cpp代码 
  1. <span style="font-size: medium;"><span style="color: #3366ff;">#include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. extern "C" void StrLower(char* pszStr)  
  7. {  
  8.     if (NULL == pszStr)  
  9.     {  
  10.         return;  
  11.     }  
  12.   
  13.     int nLen = 0;  
  14.     int i = 0;  
  15.       
  16.     nLen = strlen(pszStr);  
  17.     for (i = 0; i < nLen; i++)  
  18.     {  
  19.         if ( (pszStr[i] >= 'A') && (pszStr[i] <= 'Z') )  
  20.         {  
  21.             pszStr[i] += 26;  
  22.         }  
  23.     }  
  24. }  
  25. </span></span>  

 

 

//uppercase.cpp

 

Cpp代码 
  1. <span style="font-size: medium;"><span style="color: #3366ff;">#include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. extern "C" void StrUpper(char* pszStr)  
  7. {  
  8.     if (NULL == pszStr)  
  9.     {  
  10.         return;  
  11.     }  
  12.   
  13.     int nLen = 0;  
  14.     int i = 0;  
  15.       
  16.     nLen = strlen(pszStr);  
  17.     for (i = 0; i < nLen; i++)  
  18.     {  
  19.         if ( (pszStr[i] >= 'a') && (pszStr[i] <= 'z') )  
  20.         {  
  21.             pszStr[i] -= 26;  
  22.         }  
  23.     }  
  24. }</span>  
  25. </span>  

 

抱歉!评论已关闭.