一、静态库lib文件的生成
1.文件 -- 新建项目 -- Visual C++ -- win32项目,输入项目名称,例如:CMath。
2.项目右键 -- 添加 -- 新建项
CMath.h
class CMath
{
public:
CMath();
~CMath();
void setX(int x);
void setY(int y);
void print();
private:
int x;
int y;
};
CMath.cpp
#include "stdafx.h"
#include "CMath.h"
#include <iostream>
using namespace std;
CMath::CMath()
{
}
CMath::~CMath()
{
}
void CMath::setX(int x)
{
this->x = x;
}
void CMath::setY(int y)
{
this->y = y;
}
void CMath::print( )
{
cout << "x = " << x << ",y = " << y << endl;
}
3.生成 -- 生成解决方案
Debug 目录下生成 CMath.lib 文件
二、静态库lib文件的使用
1.新建一个项目。
2.将CMath.h和CMath.lib拷贝到新项目目录下。
3.项目 -- 右键属性 -- 连接器
-- 常规 -- 附加库目录(把CMath.lib目录添加进去)
-- 输入 -- 附加依赖项 (把CMath.lib添加进去)
4.CMain.h
#include "CMath.h"
#include <iostream>
using namespace std;
int main(int argc, char argv[])
{
CMath cmath;
cmath.setX(3);
cmath.setY(4);
cmath.print();
getchar();
}