库最基本的使用 就是用来封装代码的。
3.3 静态库
静态库是以嵌入的方式在程序中或动态库中运行。
3.1 静态库的制作
1)新建一个静态库工程项目
2)直接下一步
3)选择应用程序类型为静态库
4)1.1在头文件夹目录中新建一个.h文件并插入如下代码:
#ifndef CALCULATION_H
#define CALCULATION_H
class Calculation
{
public:
Calculation();
int add(int a,int b);
~Calculation();
};
#endif
1.2在源文件夹目录中新建一个.cpp文件并插入如下代码:
#include "Calculation.h"
Calculation::Calculation()
{
}
int Calculation::add(int a,int b)
{
return a + b;
}
Calculation::~Calculation()
{
}
3.2 静态库的使用
同制作静态库的流程大概一致,只需把应用程序类型选择为控制台应用程序即可。测试代码如下:
#include "Calculation.h"
#include <cstdio>
#include <Windows.h>
#pragma comment(lib,"staticLibrary.lib") //静态库所在的路径
int main()
{
Calculation c; //定义一个Calculation类
int ret = c.add(2,2); //使用Calculation中类的成员方法
printf("%d\n",ret);
system("pause");
return 0;
}
测试结果如下:
3.3 总结
静态库程序的特点:
- 运行时不存在;
- 链接到可执行文件或动态库中;