vs2010创建和使用动态链接库(dll)
***************************************************
更多精彩,欢迎进入:http://shop115376623.taobao.com
***************************************************
本文将创建一个简单的动态链接库,并编写一个应用台控制程序使用该动态链接库,并提出了与实现相关的几个问题,供初学者交流。
本文包含以下内容:
1、创建动态链接库项目
2、向动态链接库添加类
3、创建引用动态链接库的应用程序
4、在控制台应用程序中使用类库的功能
5、更丰富的simpledll类和相关问题
参考资料
一、创建动态链接库项目:
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、引用simpledll项目。右键单击usesimpledll项目,选择Properties->Common Properties->Framework and References。点击Add New Reference,选择simpledll项目,单击OK。
4、设置头文件路径。选择Properties->Configuration Properties->VC++ Directories。在Include Directories项添加$(SolutionDir)\simpledll\,选择应用,确定。
5、设置usesimpledll项目为活动项目。右键单击usesimpledll项目,选择Set up StartUp Project。
6、生成解决方案。Debug运行结果如下:
- 3+5=8
- 5+5=10
五、更丰富的simpledll类和相关问题:
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); //简单方法
- static int sub(int x, int y);//静态方法
- int getConst(); //
- int getNum();
- private:
- void setNum(int n);
- int num;
- };
- //全局变量
- int DLL_API number;
- SimpleDll DLL_API sdd;
- //对于指针,下面两种用法没区别?
- SimpleDll DLL_API *psdd;
- SimpleDll *psdd1;
- //方法
- int DLL_API Add(int a, int b);
- SimpleDll *createClass()
- {
- return new SimpleDll;
- }
- /*
- //问题1:若这样使用,则出现如下错误:
- // error C2059: syntax error : '__declspec(dllexport)'
- // error C2143: syntax error : missing ';' before '{'
- // error : '__declspec(dllimport)'
- // error C2143: syntax error : missing ';' before '{'
- // error C2447: '{' : missing function header (old-style formal list?)
- //为什么?
- SimpleDll* DLL_API createClass1()
- {
- return new SimpleDll;
- }
- */
- /*
- //问题2:若这样使用,则出现如下错误:
- //Error 1 error C2491: 'zdd::createClass1' : definition of dllimport function not allowed usesimpledll
- //为什么?
- SimpleDll DLL_API * createClass2()
- {
- return new SimpleDll;
- }
- */
- //问题3:这样使用(实现在.cpp中),编译没有问题。
- //但在main中应用时回出现以下错误:
- // error LNK2019: unresolved external symbol "class zdd::SimpleDll * __cdecl zdd::createClass3(void)" (?createClass3@zdd@@YAPAVSimpleDll@1@XZ) referenced in function _main
- //该如何解决?
- SimpleDll *createClass3(); //先别这样用
- //全局方法加DLL_API和不加DLL_API时的区别
- int DLL_API getConst1();
- //int getConst2(); //不要这样使用
- //也不要这样用
- //否则当程序发布之后,如果只想通过更新.dll
- //来达到更新程序数据的目的,比如将此处的100更新成10.
- //通过下面的方法你是做不到的
- int getConst2()
- {
- return 100;
- // return 10;
- }
- //也不要这样用,否则将出现如下错误
- //error C2491: 'zdd::getConst3' : definition of dllimport function not allowed
- /*
- int DLL_API getConst3()
- {
- return 100;
- }
- */
- //不要这样用,同理
- int others(int a)
- {
- return a+10;
- }
- }
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;
- }
- int SimpleDll::sub(int x, int y)
- {
- return x-y;
- }
- int SimpleDll::getConst()
- {
- return 10; //
- }
- void SimpleDll::setNum(int n)
- {
- num = n;
- }
- int SimpleDll::getNum()
- {
- return num;
- }
- extern int number = 5;
- int Add(int a, int b)
- {
- return a+b;
- }
- SimpleDll *createClass3()
- {
- return new SimpleDll;
- }
- int getConst1()
- {
- return 100;
- //return 10;
- }
- /*
- //error
- extern int getConst2()
- {
- return 100;
- }
- */
- }
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 << "SimpleDll::sub: 2-1=" << SimpleDll::sub(2,1)<<endl;
- cout << "----------------------" <<endl;
- cout << "zdd::number: "<<number<<endl;
- number = 10;
- cout << "changed zdd::number: "<<number<<endl;
- cout << "----------------------" <<endl;
- cout << "sdd.add: 6+8=" << sdd.add(6,8)<<endl;
- cout << "psdd->add: 6+8=" <<psdd->add(6,8)<<endl;
- cout << "psdd1->add: 6+8=" <<psdd1->add(6,8)<<endl;
- cout <<endl;
- cout << "sdd.getConst(): "<<sd.getConst()<<endl;
- cout << "psdd.getConst(): "<<psdd->getConst()<<endl;
- cout << "psdd1.getConst(): "<<psdd1->getConst()<<endl;
- cout << "----------------------" <<endl;
- cout << "zdd::Add: 7+8="<<Add(7,8)<<endl;
- cout << "----------------------" <<endl;
- SimpleDll *p = createClass();
- cout << "create->add: 2+3=" <<p->add(3,2)<<endl;
- cout << "create->getConst(): "<<p->getConst()<<endl;
- cout << "----------------------" <<endl;
- // SimpleDll *p3 = createClass3();
- // cout << "create3->add: 2+3=" <<p3->add(3,2)<<endl;
- // cout << "create3->getConst(): "<<p3->getConst()<<endl;
- // cout << "----------------------" <<endl;
- cout << "----------------------" <<endl;
- //请别在你的应用程序中使用getConst2
- cout << "DLL_API getConst1: "<<getConst1()<<endl;
- cout << " getConst2: "<<getConst2()<<endl;
- // cout << "DLL_API getConst3: "<<getConst3()<<endl;
- cout << "----------------------" <<endl;
- // cout << "others: " << others(6)<<endl;
- // cout << "others: " << others(16)<<endl;
- // cout << "----------------------" <<endl;
- cout << "please press Enter exit."<<endl;
- getchar();
- return 0;
- }
运行结果为:
- ----------------------
- sd.add: 3+5=8
- sd.getConst(): 10
- psd->add: 5+5=10
- psd->getConst():
- ----------------------
- SimpleDll::sub: 2-1=1
- ----------------------
- zdd::number: 5
- changed zdd::number: 10
- ----------------------
- sdd.add: 6+8=14
- psdd->add: 6+8=14
- psdd1->add: 6+8=14
- sdd.getConst(): 10
- psdd.getConst(): 10
- psdd1.getConst(): 10
- ----------------------
- zdd::Add: 7+8=15
- ----------------------
- create->add: 2+3=5
- create->getConst(): 10
- ----------------------
- ----------------------
- DLL_API getConst1: 100
- getConst2: 100
- ----------------------
- please press Enter exit.
可将生成的可执行文件和应用程序拷出到同一目录下,通过更改方法getConst1,getConst2,体会dllexport和dllimport的作用。
vs2010创建和使用动态链接库(dll)相关推荐
- VC++6.0如何创建与调用动态链接库(dll)
VC++支持的DLL: DLL的编制与具体的编程语言及编译器无关,动态链接库随处可见,VC++支持三种DLL:非MFC动态库.MFC规则DLL和MFC扩展DLL.DLL导出函数(或变量.类)可供应用程 ...
- c++ 编译添加dll_(windows平台下)深入详解C++创建动态链接库DLL以及如何使用它(一)...
前言:C以及C++的动态链接库和静态链接库,说起来很简单,但是实际上在创建的过程中有很多的坑,本人也是一路踩了很多坑,查了很多资料,下决定写一篇完整的文章来详细解释使用VS创建C++动态链接库的完整流 ...
- VC++动态链接库(DLL)编程(四)――MFC扩展 DLL
VC++动态链接库(DLL)编程(四) ――MFC扩展 DLL 作者:宋宝华 e-mail:21cnbao@21cn.com 前文我们对非MFC DLL和MFC规则DLL进行了介绍,现在开始详细 ...
- VC++动态链接库(DLL)编程(一)――理解库
VC++动态链接库(DLL)编程(一) ――理解库 作者:宋宝华 e-mail:21cnbao@21cn.com 1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概 ...
- 动态链接库dll,静态链接库lib, 导入库lib
目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称"静态库"),另一种为动态连接库(DLL,以下简称"动态库")的导入库(Imp ...
- 动态链接库dll,静态链接库lib, 导入库lib 转
动态链接库dll,静态链接库lib, 导入库lib 在用VS编译工程的时候,我们会选择动态链接库dll,静态链接库lib(static library),可是为什么在编译动态链接库的时候也可以指定输出 ...
- VC++动态链接库(DLL)编程深入浅出(zz)
1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了" ...
- Clion生成动态链接库.dll
今天研究如何生成动态链接库.dll文件纠结了好久.在保证代码文件不报错的情况下,可能要注意几个方面. 一·项目结构要完整. Clion项目生成后会和其他编程工具不同的地方在于会有一个CMakelist ...
- [转]C++学习:VC++动态链接库(DLL)编程深入浅出(zz)
转自:http://www.cnblogs.com/chio/archive/2007/11/03/948480.html 1.概论 先来阐述一下DLL(Dynamic Linkable Librar ...
最新文章
- VC++ 保存数据为音频文件(WAV)学习
- SIFT特征及特征匹配:SIFT and feature matching
- android jdk环境的配置
- Android,XML解析
- 【Flink】 producer attempted to use a producer id which is not currently assigned to its transaction
- java面试准备---JSF系统学习知识点总结---随时更新
- android统一错误ui展示,Android UI异常分析
- 谁敢动英伟达的奶酪?AI芯片领域,这12家创业公司值得关注
- Android OpenGL ES 开发教程(24):Depth Buffer
- maven构建ssm工程
- 鸿蒙推送荣耀,华为鸿蒙首批推送机型8款,荣耀“避嫌”,不在首批名单
- python第三篇:python、flask关系映射
- NIO的基本概念和缓冲区
- 最小生成树 刘汝佳模板
- 在阿里 AI Lab 做 NLP 高级算法专家是一种什么样的体验?
- ansible问题记录--Timeout (12s) waiting for privilege escalation prompt
- APP分享多张图片和文字到微信朋友圈(android 7.0以上适配)
- input的几种禁用方法
- for 和 for...in 和 for...of
- 字节跳动秋招提前批(计算机视觉工程师)