C++封装DLL,导出一个类

来源:互联网 发布:人工智能教学百度云 编辑:程序博客网 时间:2024/05/21 17:00


 

http://blog.csdn.net/yyzsyx/article/details/6034332

 

http://blog.csdn.net/yyzsyx/article/details/6034441

 

首先:建一个DLL工程,win32项目--DLL--确定;

 

在头文件中定义要导出的类:

  1. class  _declspec(dllexportCWordSegment  
  2.  
  3. public 
  4.     CWordSegment();  
  5.     ~CWordSegment();  
  6.     bool CWordSegmentInit();  
  7.     char *CWordSegmentResolve();  
  8.   
  9. };

在相应的.cpp文件中实现:

 

  1. #include "CWordSegment.h"  
  2. #include   
  3.   
  4.  CWordSegment::CWordSegment()  
  5.  
  6.     printf("CWordSegment constructed /n");  
  7.  
  8.   
  9. CWordSegment::~CWordSegment()  
  10.  
  11.     printf("CWordSegment disconstructed /n");  
  12.  
  13.   
  14.  bool CWordSegment::CWordSegmentInit()  
  15.  
  16.     printf("CWordSegmentInit /n");  
  17.     return true 
  18.  
  19.   
  20. charCWordSegment::CWordSegmentResolve()  
  21.  
  22.     printf("CWordSegmentResolve /n");  
  23.     return NULL;  
  24. }

 

三、编译源文件(快捷键F7),在../CWordSegment/Debug目录下生成DLL的两个最终要的文件,这个应该不用解释了吧

CWordSegment.dll

CWordSegment.lib 

 

  就此,DLL就搞好了,

 

 

下面如何调用这个lib文件:

 

 

也很简单:建一个测试工程控制台应用程序:

 

然后把上面生产的库文件加进来,可以用相对路径也可以用绝对路径;这个随便;

一般相对路径加入的方法:

 

(1)链接器-->常规-->附加依赖目录 加入进来即可;

(2)链接器-->常规-->附加依赖项 加入lib库名称;

然后就是导出类:

 

  1. 源码文件:testDll.h 
  2. 此处声明导入类,把第一节CWordSegment.h文件中的类定义COPY过来, 
  3. 然后把导出改为导入,即更改宏:  
  4. 由_declspec(dllexport) 导出宏--->> 改为 
  5.           --->>>  _declspec(dllimport) 倒入宏 
  6. 很简单吧 :) 
  7. */  
  8.   
  9. class  _declspec(dllimportCWordSegment  
  10.  
  11. public 
  12.     CWordSegment();  
  13.     ~CWordSegment();  
  14.     bool CWordSegmentInit();  
  15.     char *CWordSegmentResolve();  
  16. }; 

 

测试程序如下:

 

 

[cpp] view plaincopy
  1. // testDll.cpp Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6.   
  7. #include "testDll.h"   
  8.   
  9.   
  10.   
  11. CWordSegment test;  
  12.   
  13. int main(int argc, charargv[])  
  14.  
  15.       
  16.     test.CWordSegmentInit();  
  17.     printf("Hello World!/n");  
  18.     return 0;  

 

http://blog.csdn.net/yyzsyx/article/details/6034332

 

http://blog.csdn.net/yyzsyx/article/details/6034441

 

首先:建一个DLL工程,win32项目--DLL--确定;

 

在头文件中定义要导出的类:

  1. class  _declspec(dllexportCWordSegment  
  2.  
  3. public 
  4.     CWordSegment();  
  5.     ~CWordSegment();  
  6.     bool CWordSegmentInit();  
  7.     char *CWordSegmentResolve();  
  8.   
  9. };

在相应的.cpp文件中实现:

 

  1. #include "CWordSegment.h"  
  2. #include   
  3.   
  4.  CWordSegment::CWordSegment()  
  5.  
  6.     printf("CWordSegment constructed /n");  
  7.  
  8.   
  9. CWordSegment::~CWordSegment()  
  10.  
  11.     printf("CWordSegment disconstructed /n");  
  12.  
  13.   
  14.  bool CWordSegment::CWordSegmentInit()  
  15.  
  16.     printf("CWordSegmentInit /n");  
  17.     return true 
  18.  
  19.   
  20. charCWordSegment::CWordSegmentResolve()  
  21.  
  22.     printf("CWordSegmentResolve /n");  
  23.     return NULL;  
  24. }

 

三、编译源文件(快捷键F7),在../CWordSegment/Debug目录下生成DLL的两个最终要的文件,这个应该不用解释了吧

CWordSegment.dll

CWordSegment.lib 

 

  就此,DLL就搞好了,

 

 

下面如何调用这个lib文件:

 

 

也很简单:建一个测试工程控制台应用程序:

 

然后把上面生产的库文件加进来,可以用相对路径也可以用绝对路径;这个随便;

一般相对路径加入的方法:

 

(1)链接器-->常规-->附加依赖目录 加入进来即可;

(2)链接器-->常规-->附加依赖项 加入lib库名称;

然后就是导出类:

 

  1. 源码文件:testDll.h 
  2. 此处声明导入类,把第一节CWordSegment.h文件中的类定义COPY过来, 
  3. 然后把导出改为导入,即更改宏:  
  4. 由_declspec(dllexport) 导出宏--->> 改为 
  5.           --->>>  _declspec(dllimport) 倒入宏 
  6. 很简单吧 :) 
  7. */  
  8.   
  9. class  _declspec(dllimportCWordSegment  
  10.  
  11. public 
  12.     CWordSegment();  
  13.     ~CWordSegment();  
  14.     bool CWordSegmentInit();  
  15.     char *CWordSegmentResolve();  
  16. }; 

 

测试程序如下:

 

 

[cpp] view plaincopy
  1. // testDll.cpp Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6.   
  7. #include "testDll.h"   
  8.   
  9.   
  10.   
  11. CWordSegment test;  
  12.   
  13. int main(int argc, charargv[])  
  14.  
  15.       
  16.     test.CWordSegmentInit();  
  17.     printf("Hello World!/n");  
  18.     return 0;