本文主要介绍了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++详细实现完整图书管理功能 2023-04-04
- 如何C++使用模板特化功能 2023-03-05
- 一文带你了解C++中的字符替换方法 2023-07-20
- C/C++编程中const的使用详解 2023-03-26
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- C利用语言实现数据结构之队列 2022-11-22
- C++中的atoi 函数简介 2023-01-05
- 详解c# Emit技术 2023-03-25
- C语言 structural body结构体详解用法 2022-12-06
- C语言基础全局变量与局部变量教程详解 2022-12-31