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

参考他人的基础上自己的总结 

一、创建动态链接库项目: 

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

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

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

4、单击OK,在出现的Win32 Application Wizard的Overview对话框中点击Next。 

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

6、勾选Additional options下的Empty project。 

7、单击Finish创建项目。 


向动态链接库添加类: 

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

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

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; 




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

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

1、选择File->New->Project。 

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

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

4、单击OK,在出现的Win32 Application Wizard的Overview对话框中点击Next。 

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

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

7、单击Finish创建项目。 


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

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

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

//------------------ main.cpp ------------------- 

#include "simpledll.h" 
using namespace zdd; 
#include 
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; 


3.在工程目录下建立Include目录,将动态链接库的那个头文件拷入。建立lib目录,将生成的那个.lib文件拷入。然后将生成的.dll文件拷入生成.exe文件的那个目录(一般是项目下的Debug下)。 

4.程序中要包含那个头文件,注意路径要写正确。Include “..\Include\simpledll.h”,或者右击工程,property,Configuration Properties,c/c++,General,在Additional Include Directories中加入“;..\Include”,这样包含头文件时直接写头文件名,不需要考虑路径,因为当在工程目录下找不到文件时,就会从添加的那个目录查找文件。 

5.添加.lib文件 

右击工程,property,Configuration Properties,Linker,Input,在Additional Dependencies中添加.lib路径(一般是..\lib\xxxxx.lib)。 


另外,lib引用有两种方法: 
1.#pragma comment(lib,”opengl32.lib”) 
2.选择project –> XX properties… –> linker –> Input –> Additional dependences,在其中加入lib文件名即可。 


总结: 
首先建立生成DLL的工程,生成.dll,.lib文件。需要用到的还有.h文件。 
建立应用DLL的工程。要包含头文件,把3个文件拷入相应的目录。 
在附加依赖项Additional Dependencies中添加.lib的路径,告诉程序调用的外部导入函数的地址,否则找不到函数,链接出错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值