TearsOfDawn

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

//CWordSegment.h
//在头文件中定义要导出的类: class _declspec(dllexport) CWordSegment { public: CWordSegment(); ~CWordSegment(); bool CWordSegmentInit(); char *CWordSegmentResolve(); };

 

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

 

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

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

CWordSegment.dll

CWordSegment.lib 

 就此,DLL就搞好了。。。。

 

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

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

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

 

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

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

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

 

然后就是导出类:

//源码文件:testDll.h 
//此处声明导入类,把第一节CWordSegment.h文件中的类定义COPY过来, 
//然后把导出改为导入,即更改宏:  
//由 _declspec(dllexport) 导出宏 改为 _declspec(dllimport) 导入宏 
//很简单吧 :) 

class  _declspec(dllimport) CWordSegment  
{  
public:  
    CWordSegment();  
    ~CWordSegment();  
    bool CWordSegmentInit();  
    char *CWordSegmentResolve();  
}; 

测试程序如下:

 1  //[cpp]
 2  // testDll.cpp : Defines the entry point for the console application.  
 3  #include "stdafx.h"  
 4  #include "testDll.h"   
 5    
 6  CWordSegment test;  
 7    
 8  int main(int argc, char* argv[])  
 9  {  
10       
11      test.CWordSegmentInit();  
12      printf("Hello World!/n");  
13      return 0;  
14  } 

 

 不是专业的程序猿,脑子经常不够用,就记录在这里,用的时候过来看看。

必须声明:这篇文章是我从其他博文网站复制过来的,只是修改了一下版式,方便阅读。如有侵权,敬请联系!

 

分类:

技术点:

C++

相关文章: