C++DLL动态库的创建与调用(类库,隐式调用)

Jacuqeline ·
更新时间:2024-09-21
· 1688 次阅读

目录

1、创建库工程

2、添加头文件

3、添加cpp文件

4、编译dll工程

5、创建调用工程

6、调用工程 添加cpp文件

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动态库内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



dll c+ 调用 类库

需要 登录 后方可回复, 如果你还没有账号请 注册新账号