C++封装库的详细教程

1使用MSVC封装库文件

1.1配置

.h文件需要配置,导入导出以及平台设置

#ifdef CLASS_LIBRAR
    #define CLASS_EXPORT __declspec(dllexport)
#else
    #define CLASS_EXPORT __declspec(dllimport)
#endif

.h文件配置

#ifdef __cplusplus
extern "C" { //only need to export C interface if used by C++ source code 
#endif

__declspec(dllimport) void MyCFunc();
__declspec(dllimport) void AnotherCFunc();

#ifdef __cplusplus
}
#endif

.cpp文件函数正常编写

2在Visual Studio 2022中成功封装并调用库的案例

2.1 封装教程

第一步 创建动态库项目

​ 创建新项目——》选择动态库模板

在这里插入图片描述

第二步 删除自带文件

​ 创建成功后,删除自带的头文件和源文件

第三步 配置属性

​ 选中项目右键属性——》选择C/C++——》选择“预编译头”—>预编译头,设置成不使用

在这里插入图片描述

第四步 添加预处理器定义

​ 添加预处理器定义(DLL_BUILD),后面编写头文件用得上

在这里插入图片描述

第五步 创建自己的代码

​ .h文件

#pragma once

//dll部署操作
#ifdef DLL_BUILD
//标识为导出操作
#define DLL_API _declspec(dllexport)
#else
//标识为引入操作
#define DLL_API _declspec(dllimport)
#endif

#include <vector>

using namespace std;

const int null = 11111111;

//Definition for a binary tree node.
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

//编译器识别
#ifdef __cplusplus
extern "C" {
#endif

	//通过数组转换成对应的二叉树
	DLL_API TreeNode* generateBinaryTree(vector<int>& nodes);

	//测试函数是否封装成功 1:成功
	DLL_API int success();

#ifdef __cplusplus
}
#endif // __cplusplus



.cpp文件

#include "CreateBiTree.h"

#include <queue>

//null:11111111
TreeNode* generateBinaryTree(vector<int>& nodes)
{
	int len = nodes.size();
	if (len == 0)return NULL;
	queue<TreeNode*> q; 
	TreeNode* root = new TreeNode(nodes[0]);
	q.push(root);
	int index = 1;
	
	while (!q.empty()&&index<len) //填充孩子
	{
		TreeNode* node = q.front();
		q.pop();
		if (index < len) //添加左孩子
		{
			int leftVal = nodes[index++];
			if (leftVal != null)
			{
				TreeNode* leftChild = new TreeNode(leftVal);
				node->left = leftChild;
				q.push(leftChild);
			}	
		}
		if (index < len) //添加左孩子
		{
			int rightVal = nodes[index++];
			if (rightVal != null)
			{
				TreeNode* rightChild = new TreeNode(rightVal);
				node->right = rightChild;
				q.push(rightChild);
			}
		}
		
	}
	return root;
}

int success()
{
	return 1;
}

第六步 点击生成

(库项目不可以运行哦)

在这里插入图片描述

最后成功后,会生成.dll文件和.lib文件,记得使用它时,把头文件也带走哦

在这里插入图片描述

2.2 调用dll教程

第一步 创建空项目

​ 创建一个空项目,并创建一个main函数用于测试。

​ 在项目下创建两个文件夹include和bin,分别用于存放.h文件和dll及lib文件

第二步 配置属性

​ 配置exe的输出目录,注意c++项目的根路径相对于**.vcxproj**文件

在这里插入图片描述

第三步 引用头文件

就是封装dll使用的头文件所在目录:CreateBinaryTree.h

在这里插入图片描述

第四步 引用库目录

就是 dll所在目录CreateBinaryTree.dll

在这里插入图片描述

第五步 引用lib文件

在连接器—》输入-----》附加依赖项—》双击编辑添加CreateBinaryTree.lib

在这里插入图片描述

第六步 添加头文件,即可调用封装的函数了

在这里插入图片描述

输出结果:

在这里插入图片描述

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 是一种面向对象的编程语言,它是 C 语言的扩展,支持面向对象编程、泛型编程、模板和异常处理等特性。下面是一个简单的 C++ 教程,介绍 C++ 基础语法和常用概念。 ### Hello World 让我们从经典的 Hello World 程序开始: ```c++ #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; } ``` 这个程序使用了 `iostream` 标准,定义了一个 `main()` 函数,输出了字符串 "Hello World!"。 ### 变量和数据类型 C++ 支持多种数据类型,例如整型、浮点型、字符型、布尔型等。在使用变量之前,需要先定义它们的数据类型和名称。 ```c++ int i = 10; // 整型 float f = 3.14; // 单精度浮点型 double d = 3.1415926; // 双精度浮点型 char c = 'A'; // 字符型 bool b = true; // 布尔型 ``` ### 运算符 C++ 支持多种运算符,例如算术运算符(`+`、`-`、`*`、`/`、`%`)、关系运算符(`<`、`>`、`<=`、`>=`、`==`、`!=`)、逻辑运算符(`&&`、`||`、`!`)等。运算符的优先级也需要注意。 ```c++ int a = 10, b = 20; int c = a + b; // 加法运算 int d = a * b; // 乘法运算 bool e = a > b; // 关系运算 bool f = !e; // 逻辑运算 ``` ### 控制流程 C++ 支持多种控制流程语句,例如条件语句(`if-else`)、循环语句(`for`、`while`、`do-while`)、跳转语句(`break`、`continue`、`goto`)等。这些语句可以帮助程序根据不同的条件执行不同的代码。 ```c++ int n = 10; if (n > 0) { cout << "n is positive" << endl; } else { cout << "n is negative" << endl; } for (int i = 0; i < n; i++) { cout << i << endl; } while (n > 0) { cout << n << endl; n--; } do { cout << n << endl; n++; } while (n < 10); switch (n) { case 1: cout << "n is 1" << endl; break; case 2: cout << "n is 2" << endl; break; default: cout << "n is neither 1 nor 2" << endl; } ``` ### 函数和类 C++ 支持函数和类的定义,这是面向对象编程的基础。函数是一段完成特定任务的代码,可以在程序的任何地方调用它们。类是一种封装了数据和方法的数据类型,用于描述对象的行为和属性。 ```c++ // 函数定义 int add(int a, int b) { return a + b; } // 类定义 class Student { public: string name; int age; void study() { cout << "I am studying." << endl; } void sleep() { cout << "I am sleeping." << endl; } }; ``` ### 模板和异常处理 C++ 还支持模板和异常处理等高级特性。模板是一种泛型编程的技术,可以在编写代码时不指定具体的数据类型。异常处理是一种机制,用于在程序出现错误时捕获并处理异常,防止程序崩溃。 ```c++ // 模板定义 template<typename T> T add(T a, T b) { return a + b; } // 异常处理 try { int x = 10, y = 0; if (y == 0) { throw "Division by zero!"; } int z = x / y; } catch (const char* msg) { cerr << msg << endl; } ``` 这是一个简单的 C++ 教程,介绍了 C++ 基础语法和常用概念。如果想深入学习 C++,可以阅读更多相关的书籍和教程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值