VS2010生成和使用链接库

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

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 <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; 


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的路径,告诉程序调用的外部导入函数的地址,否则找不到函数,链接出错。 
目录
相关文章
|
3月前
|
消息中间件 NoSQL Linux
静态库与动态库
静态库与动态库
静态库与动态库
|
5月前
|
存储 Cloud Native Linux
CMake学习之静态库动态库
CMake学习之静态库动态库
|
6月前
|
存储 Linux 编译器
什么是链接库 | 动态库与静态库
什么是链接库 | 动态库与静态库
79 0
|
7月前
|
Linux API C语言
编译参数中如何包含头文件和动态链接库
GCC编译参数:如何包含头文件和动态链接库
49 0
|
11月前
|
vr&ar C语言 索引
静态链接和静态库
静态链接和静态库
126 0
|
Shell 开发工具 C语言
动态库与静态库
本文目标:⭐认识动态静态库,学会结合gcc选项,制作动静态库⭐⭐了解动态库加载过程⭐。
动态库与静态库
|
C语言
使用链接库
使用链接库
90 0
|
vr&ar C语言
|
存储 缓存 Shell
动态库与静态库上 (3)
动态库与静态库上 (3)
173 0
动态库与静态库上 (3)