vs2010创建和使用动态链接库(dll)

vs2010创建和使用动态链接库(dll)

本文将创建一个简单的动态链接库,并编写一个应用台控制程序使用该动态链接库,并提出了与实现相关的几个问题,供初学者交流。

本文包含以下内容:

创建动态链接库项目

向动态链接库添加类

创建引用动态链接库的应用程序

在控制台应用程序中使用类库的功能

更丰富的simpledll类和相关问题

参考资料


1.创建动态链接库项目:

1.1、打开Microsoft Visual Studio 2010,选择File->New->Project

1.2、在New Project中选择Installed Templates->Visual C++->Win32

1.3、选择Win32 Console Application,设置名称:simpledll,设置解决方案名:zdddll

1.4、单击OK,在出现的Win32 Application WizardOverview对话框中点击Next

1.5、在Application Settings中,选择Application type下的DLL

1.6、勾选Additional options下的Empty project

1.7、单击Finish创建项目。


2.向动态链接库添加类:

2.1、添加新类头文件。右键单击simpledll项目,Add->New Item,选择Header File(.h),设置名称为simpledll,单击Add

2.2、添加新类源文件。右键单击simpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为simpledll,单击Add

2.3、为新类添加内容。内容如下:

头文件simpledll.h:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //------------------ simpledll.h ----------------  
  2.   
  3. #pragma once;  
  4.   
  5. //该宏完成在dll项目内部使用__declspec(dllexport)导出  
  6. //在dll项目外部使用时,用__declspec(dllimport)导入  
  7. //宏DLL_IMPLEMENT在simpledll.cpp中定义  
  8. #ifdef DLL_IMPLEMENT  
  9. #define DLL_API __declspec(dllexport)  
  10. #else  
  11. #define DLL_API __declspec(dllimport)  
  12. #endif  
  13.   
  14. namespace zdd  
  15. {  
  16.     //导出类  
  17.     class DLL_API SimpleDll  
  18.     {  
  19.     public:  
  20.         SimpleDll();  
  21.         ~SimpleDll();  
  22.   
  23.         int add(int x, int y); //简单方法  
  24.     };  
  25. }  

源文件simpledll.cpp:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //------------------ simpledll.cpp ----------------  
  2.   
  3. //注意此处的宏定义需要写在#include "simpledll.h"之前  
  4. //以完成在dll项目内部使用__declspec(dllexport)导出  
  5. //在dll项目外部使用时,用__declspec(dllimport)导入  
  6. #define DLL_IMPLEMENT   
  7.   
  8. #include "simpledll.h"  
  9.   
  10. namespace zdd  
  11. {  
  12.     SimpleDll::SimpleDll()  
  13.     {  
  14.   
  15.     }  
  16.   
  17.     SimpleDll::~SimpleDll()  
  18.     {  
  19.   
  20.     }  
  21.   
  22.     int SimpleDll::add(int x, int y)  
  23.     {  
  24.         return x+y;  
  25.     }  
  26. }  

2.4、完成后点击Build->Build Solution,生成解决方案。可在~zdddll\Debug下查看生成的simpledll.libsimpledll.dll.文件。


3.创建引用动态链接库的应用程序:

3.1、选择File->New->Project

3.2、在New Project中选择Installed Templates->Visual C++->Win32

3.3、选择Win32 Console Application,设置名称:usesimpledll。选择Add to solution

3.4、单击OK,在出现的Win32 Application WizardOverview对话框中点击Next

3.5、在Application Settings中,选择Application type下的Console application

3.6、取消Additional options下的Precompiled header,勾选Empty project

3.7、单击Finish创建项目。


4.在控制台应用程序中使用类库的功能:

4.1、为控制台应用程序添加main.cpp。右键单击usesimpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为main,单击Add

4.2、为main.cpp添加内容。如下所示:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //------------------ main.cpp -------------------  
  2. #include "simpledll.h"  
  3. using namespace zdd;  
  4.   
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8. int main(char argc, char**argv)  
  9. {  
  10.     //  
  11.     cout << "----------------------" <<endl;  
  12.     SimpleDll sd;  
  13.     cout << "sd.add: 3+5=" << sd.add(3, 5)<<endl;  
  14.     cout << "sd.getConst(): "<<sd.getConst()<<endl;  
  15.   
  16.     SimpleDll *psd = new SimpleDll;  
  17.     cout << "psd->add: 5+5=" << psd->add(5, 5)<<endl;  
  18.     cout << "psd->getConst(): "<<endl;  
  19.   
  20.     cout << "----------------------" <<endl;  
  21.     cout << "please press Enter exit."<<endl;  
  22.     getchar();  
  23.     return 0;  
  24. }  

4.3、引用simpledll项目。右键单击usesimpledll项目,选择Properties->Common Properties->Framework and References。点击Add New Reference,选择simpledll项目,单击OK

4.4、设置头文件路径。选择Properties->Configuration Properties->VC++ Directories。在Include Directories项添加$(SolutionDir)\simpledll\,选择应用,确定。




4.5、设置usesimpledll项目为活动项目。右键单击usesimpledll项目,选择Set up StartUp Project

4.6、生成解决方案。Debug运行结果如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 3+5=8  
  2. 5+5=10  


==================================================================================
【注意:】
【1】4.3的作用,关联工程,则是设置了LIB,如果是别人提供的lib,则需要设置路径了,具体步骤如下



【2】4.4的要注意,设置包含头文件路径,这样"simpledll.h"文件就不用移动;  一般有两种方式
                 1. VC++ Directories -> Include Directories
                2. C/C++ -> General -> Additional Include Directories
这里使用了VC++ Directories -> Include Directories,不过还是推荐使用 C/C++ -> General -> Additional Include Directories,因为VC++ Directories -> Include Directories有别设置,可能不小心修改了就不好了,而 C/C++ -> General -> Additional Include Directories是空的。


======================================================
源代码:


//=============================================================================
FROM:  
vs2010 创建和使用动态链接库 (dll)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值