编写dll 关于declspec(dllexport)和declspec(dllimport)
发布时间:2020-12-13 21:11:30 所属栏目:Windows 来源:网络整理
导读:使用api 要先懂得怎么使用dll文件; 1.新建一个常规dll //dll.h #ifndef DLL_H #define DLL_H #ifdef DLL_EXPORTS #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT __declspec(dllimport) #endif DLLEXPORT int add(int,int); #endif //中
使用api 要先懂得怎么使用dll文件;
1.新建一个常规dll //dll.h #ifndef DLL_H #define DLL_H #ifdef DLL_EXPORTS #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT __declspec(dllimport) #endif DLLEXPORT int add(int,int); #endif //中间一段的意思是: 如果在工程里添加预定义宏DLL_EXPORTS 那DLLEXPORT 就指代__declspec(dllexport) 用于dll的导出(函数 变量 类等) 导出变量用 __declspec(dllexport) int a; 导出函数用 __declspec(dllexport) void foo(); 导出类用 class __declspec(dllexport) a{}; 如果应用程序需要调用dll中的函数,则需要用__declspec(dllimport)修饰,因此当工程中不包含DLL_EXPORTS预定义时,DLLEXPORT 就指代__declspec(dllimport) 这样生成dll程序和调用dll的程序可以使用同一个头文件 (ps:工程中会自动添加一个DLL_EXPORTS预定义,在属性—cc++ —preprocesspr里面) //dll.cpp #include "dll.h" int add(int x,int y){ return x+y; } 这样在工程的debug目录下就生成了dll和lib文件 2.调用dll文件 新建一个控制台程序calldll 添加c++文件 //calldll.cpp #include "dll.h" #include <iostream> int main(){ int z; int x=1,y=2; z=add(x,y); std::cout<<z<<std::endl; } 工程属性里设置 1)cc++ - general - additional include directories里面添加头文件目录 2)linker -additional library directories 里添加lib文件的目录 3)linker -input -additonal dependencies 里添加lib的文件名 编译通过后用行,会提示丢失dll.dll文件 解决:将dll.dll放在和exe同一目录下或放在c:windowssystem32目录下(Win64位操作系统且dll.dll不是64位而是32位的话放在c:windowssyswow64目录下) 原文地址:http://blog.sina.com.cn/s/blog_a7e72e940101izkh.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- active-directory – Windows 10:具有缺失权限的AD域管理员
- 有没有办法监视哪个进程在Windows中发送UDP数据包(源/目标I
- 如何在Windows中使用带代理的weka包管理器?
- Windows – IIS7.5应用程序池回收 – .Net OutOfMemoryExce
- Windows用于检查用户名的区域设置?
- windows – 如何删除Anaconda的键盘快捷方式
- Windows Server Standard – 在Microsoft OS中显示为Window
- windows-server-2012 – 在Windows Server 2012中 – 是否可
- OpenGL 基于OpenGL的三维机器人仿真
- windows下的Redis安装:
推荐文章
站长推荐
- windows – 强制任何正在运行的进程崩溃
- windows-server-2008 – 断开连接后,RDP会话的GU
- windows-phone-8 – Longlistselector中的ItemsP
- 在Windows FF中显示边框的CSS透明边框
- windows-server-2008-r2 – 在Windows Server 20
- batch-file – 如何在Windows 10上使用带有CSCRI
- 在windows中缓存http git密码:mysysgit
- Windows XP 64位还可以购买吗?
- Windows API—CreateEvent—创建事件
- 如何将“类项目”中的dll嵌入到我在vb.net中的项
热点阅读