本文主要介绍了C++DLL动态库的创建与调用(类库,隐式调用),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
1、创建库工程


2、添加头文件
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
3、添加cpp文件
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;
}
4、编译dll工程
生成文件

5、创建调用工程
普通工程、多字节项目
6、调用工程 添加cpp文件
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动态库内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:C++ DLL动态库的创建与调用(类库,隐式调用)
基础教程推荐
猜你喜欢
- C语言数组长度的计算方法实例总结(sizeof与strlen) 2023-04-26
- 05-C语言进阶——动态内存管理 2023-11-20
- C语言植物大战数据结构二叉树递归 2023-04-09
- 纯C++代码详解二叉树相关操作 2023-05-15
- 利用QT设计秒表功能 2023-05-30
- Qt数据库应用之实现通用数据库请求 2023-03-18
- g++: const 丢弃限定符 2022-10-07
- character-encoding – Linux中最常见的C语言编码(和Unix?) 2023-11-21
- VisualStudio2010安装教程 2023-01-05
- C语言的三种条件判断语句你都了解吗 2023-03-05
