目录1、创建库工程2、添加头文件3、添加cpp文件4、编译dll工程5、创建调用工程6、调用工程 添加cpp文件1、创建库工程 2、添加头文件 ClassDll.h // 宏定义
ClassDll.h
// 宏定义 防止.h文件重复编译
#ifndef _DLLCLASS_H
#define _DLLCLASS_H
// dll库文件 定义 宏(DLLCLASS_EXPORTS) 使用 _declspec(dllexport)
// 使用dll库文件时 _declspec(dllimport)(不定义宏就行)
#ifdef DLLCLASS_EXPORTS
#define EXT_CLASS _declspec(dllexport)
#else
#define EXT_CLASS _declspec(dllimport)
#endif
// 定义库文件的 类(导出或导入)
class EXT_CLASS CMath
{
public:
// 定义函数
int Add(int item1, int item2);
int Sub(int item1, int item2);
};
#endif
ClassDll.cpp
// 定义 宏(DLLCLASS_EXPORTS) 头文件类
// 使用 _declspec(dllexport) 导出
#define DLLCLASS_EXPORTS
#include "ClassDll.h"
// 实现类函数
int CMath::Add(int item1, int item2)
{
return item1 + item2;
}
int CMath::Sub(int item1, int item2)
{
return item1 - item2;
}
生成文件
普通工程、多字节项目
UseClassdll.cpp
#include <iOStream>
using namespace std;
// 导入头文件 库类 使用 _declspec(dllimport) 导出类
#include "../ClassDll/ClassDll.h"
// 隐式调用dll 加载库文件
#pragma comment(lib, "../Debug/ClassDll.lib")
// 运行时 dll文件与exe文件在一个文件夹中
int main() {
// 定义 dll库中的类
CMath math;
// 调用函数
int sum = math.Add(5, 6);
int sub = math.Sub(5, 6);
// 打印结果
cout << "sum=" << sum << " sub=" << sub << endl;
system("pause");
return 0;
}
到此这篇关于c++ DLL动态库的创建与调用(类库,隐式调用)的文章就介绍到这了,更多相关C++ DLL动态库内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C++DLL动态库的创建与调用(类库,隐式调用)
本文链接: https://www.lsjlt.com/news/149248.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0