網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C++?DLL動(dòng)態(tài)庫(kù)的創(chuàng)建與調(diào)用(類庫(kù),隱式調(diào)用)_C 語(yǔ)言
作者:廷益--飛鳥 ? 更新時(shí)間: 2022-07-15 編程語(yǔ)言1、創(chuàng)建庫(kù)工程
2、添加頭文件
ClassDll.h
// 宏定義 防止.h文件重復(fù)編譯
#ifndef _DLLCLASS_H
#define _DLLCLASS_H
// dll庫(kù)文件 定義 宏(DLLCLASS_EXPORTS) 使用 _declspec(dllexport)
// 使用dll庫(kù)文件時(shí) _declspec(dllimport)(不定義宏就行)
#ifdef DLLCLASS_EXPORTS
#define EXT_CLASS _declspec(dllexport)
#else
#define EXT_CLASS _declspec(dllimport)
#endif
// 定義庫(kù)文件的 類(導(dǎo)出或?qū)耄?
class EXT_CLASS CMath
{
public:
// 定義函數(shù)
int Add(int item1, int item2);
int Sub(int item1, int item2);
};
#endif
3、添加cpp文件
ClassDll.cpp
// 定義 宏(DLLCLASS_EXPORTS) 頭文件類
// 使用 _declspec(dllexport) 導(dǎo)出
#define DLLCLASS_EXPORTS
#include "ClassDll.h"
// 實(shí)現(xiàn)類函數(shù)
int CMath::Add(int item1, int item2)
{
return item1 + item2;
}
int CMath::Sub(int item1, int item2)
{
return item1 - item2;
}
4、編譯dll工程
生成文件
5、創(chuàng)建調(diào)用工程
普通工程、多字節(jié)項(xiàng)目
6、調(diào)用工程 添加cpp文件
UseClassdll.cpp
#include <iostream>
using namespace std;
// 導(dǎo)入頭文件 庫(kù)類 使用 _declspec(dllimport) 導(dǎo)出類
#include "../ClassDll/ClassDll.h"
// 隱式調(diào)用dll 加載庫(kù)文件
#pragma comment(lib, "../Debug/ClassDll.lib")
// 運(yùn)行時(shí) dll文件與exe文件在一個(gè)文件夾中
int main() {
// 定義 dll庫(kù)中的類
CMath math;
// 調(diào)用函數(shù)
int sum = math.Add(5, 6);
int sub = math.Sub(5, 6);
// 打印結(jié)果
cout << "sum=" << sum << " sub=" << sub << endl;
system("pause");
return 0;
}
原文鏈接:https://blog.csdn.net/weixin_45875105/article/details/124717340
相關(guān)推薦
- 2022-09-22 初始化列表 static成員
- 2022-07-15 Python標(biāo)準(zhǔn)庫(kù)之Math,Random模塊使用詳解_python
- 2022-10-26 C#實(shí)現(xiàn)文件與字符串互轉(zhuǎn)的方法詳解_C#教程
- 2022-01-31 (數(shù)據(jù))圖像預(yù)處理——image augmentation圖像增廣之cutout、Mixup、Cut
- 2022-05-15 實(shí)例詳解Python的進(jìn)程,線程和協(xié)程_python
- 2022-06-11 python?針對(duì)在子文件夾中的md文檔實(shí)現(xiàn)批量md轉(zhuǎn)word_python
- 2022-07-06 C#多線程之線程池ThreadPool詳解_C#教程
- 2022-06-07 Python生成可執(zhí)行文件之PyInstaller庫(kù)的使用方式_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支