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 Wizard的Overview对话框中点击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:
- //------------------ simpledll.h ----------------
- #pragma once;
- //该宏完成在dll项目内部使用__declspec(dllexport)导出
- //在dll项目外部使用时,用__declspec(dllimport)导入
- //宏DLL_IMPLEMENT在simpledll.cpp中定义
- #ifdef DLL_IMPLEMENT
- #define DLL_API __declspec(dllexport)
- #else
- #define DLL_API __declspec(dllimport)
- #endif
- namespace zdd
- {
- //导出类
- class DLL_API SimpleDll
- {
- public:
- SimpleDll();
- ~SimpleDll();
- int add(int x, int y); //简单方法
- };
- }
源文件simpledll.cpp:
- //------------------ simpledll.cpp ----------------
- //注意此处的宏定义需要写在#include "simpledll.h"之前
- //以完成在dll项目内部使用__declspec(dllexport)导出
- //在dll项目外部使用时,用__declspec(dllimport)导入
- #define DLL_IMPLEMENT
- #include "simpledll.h"
- namespace zdd
- {
- SimpleDll::SimpleDll()
- {
- }
- SimpleDll::~SimpleDll()
- {
- }
- int SimpleDll::add(int x, int y)
- {
- return x+y;
- }
- }
2.4、完成后点击Build->Build Solution,生成解决方案。可在~zdddll\Debug下查看生成的simpledll.lib和simpledll.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 Wizard的Overview对话框中点击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添加内容。如下所示:
- //------------------ main.cpp -------------------
- #include "simpledll.h"
- using namespace zdd;
- #include <iostream>
- using namespace std;
- int main(char argc, char**argv)
- {
- //
- cout << "----------------------" <<endl;
- SimpleDll sd;
- cout << "sd.add: 3+5=" << sd.add(3, 5)<<endl;
- cout << "sd.getConst(): "<<sd.getConst()<<endl;
- SimpleDll *psd = new SimpleDll;
- cout << "psd->add: 5+5=" << psd->add(5, 5)<<endl;
- cout << "psd->getConst(): "<<endl;
- cout << "----------------------" <<endl;
- cout << "please press Enter exit."<<endl;
- getchar();
- return 0;
- }
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运行结果如下:
- 3+5=8
- 5+5=10
2. C/C++ -> General -> Additional Include Directories