创建和使用静态库Lib


创建静态库项目

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

  • 1
  • 2
  • 下一页

相关内容