日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(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ù)工程

請(qǐng)?zhí)砑訄D片描述

請(qǐng)?zhí)砑訄D片描述

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

欄目分類
最近更新