網站首頁 編程語言 正文
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;
}
原文鏈接:https://blog.csdn.net/weixin_45875105/article/details/124717340
相關推薦
- 2022-06-10 ASP.NET?Core使用EF保存數據、級聯刪除和事務使用_實用技巧
- 2024-01-09 poi操作Excel給列設置下拉菜單(數據驗證)
- 2023-05-30 關于keras中卷積層Conv2D的學習記錄_python
- 2022-12-27 React生命周期函數圖解介紹_React
- 2022-09-16 Golang實現Biginteger大數計算實例詳解_Golang
- 2022-05-06 python 實現兩個excel表格數據的對比
- 2022-05-06 如何利用Go語言實現LRU?Cache_Golang
- 2022-08-11 boost字符串處理函數format的用法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支