創建靜態庫項目
1.創建靜態項目MathFucsLib:
選擇 win32控制台應用程序->輸入MathFuncsLib項目名稱->下一步->選擇 靜態庫;取消 默認的預編譯頭->完畢
2.向靜態庫添加類MyMathFuncs:
#pragma once
//MathFuncsLib.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
static double Add(double a,double b);
static double Substract(double a,double b);
static double Multiply(double a,double b);
static double Divide(double a,double b);
};
}
//MathFuncsLib.cpp
#include "MathFuncsLib.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a,double b)
{
return a+b;
}
double MyMathFuncs::Substract(double a,double b)
{
return a-b;
}
double MyMathFuncs::Multiply(double a,double b)
{
return a*b;
}
double MyMathFuncs::Divide(double a,double b)
{
if (b==0)
{
throw new invalid_argument("b cannot be zero!");
}
return a/b;
}
}
3.確認生成的是lib文件:項目,屬性->配置屬性,常規->配置類型:改為靜態庫(.lib);編譯生成MathFuncsLib.lib