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

學無先后,達者為師

網站首頁 編程語言 正文

C++?DLL動態庫的創建與調用(類庫,隱式調用)_C 語言

作者:廷益--飛鳥 ? 更新時間: 2022-07-15 編程語言

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

欄目分類
最近更新