# MFC - 库

• 静态库
• 动态库

## 静态库

静态库是一个文件，其中包含外部程序可以用来补充其功能的函数，类或资源，要使用库，程序员必须创建一个链接，该项目可以是控制台应用程序，Win32或MFC应用程序，库文件具有lib扩展名。

```#pragma once
#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_
double Min(const double *Numbers, const int Count);
double Max(const double *Numbers, const int Count);
double Sum(const double *Numbers, const int Count);
double Average(const double *Numbers, const int Count);
long GreatestCommonDivisor(long Nbr1, long Nbr2);
#endif //_CALCULATOR_H_```

```#include "StdAfx.h"
#include "Calculator.h"
double Min(const double *Nbr, const int Total) {
double Minimum = Nbr[0];
for (int i = 0; i < Total; i++)
if (Minimum > Nbr[i])
Minimum = Nbr[i];
return Minimum;
}
double Max(const double *Nbr, const int Total) {
double Maximum = Nbr[0];
for (int i = 0; i < Total; i++)
if (Maximum < Nbr[i])
Maximum = Nbr[i];
return Maximum;
}
double Sum(const double *Nbr, const int Total) {
double S = 0;
for (int i = 0; i < Total; i++)
S += Nbr[i];
return S;
}
double Average(const double *Nbr, const int Total) {
double avg, S = 0;
for (int i = 0; i < Total; i++)
S += Nbr[i];
avg = S/Total;
return avg;
}
long GreatestCommonDivisor(long Nbr1, long Nbr2) {
while (true) {
Nbr1 = Nbr1 % Nbr2;
if (Nbr1 == 0)
return Nbr2;
Nbr2 = Nbr2 % Nbr1;
if (Nbr2 == 0)
return Nbr1;
}
}```

```#include "stdafx.h"
#include "MFCLibTest.h"
#include "MFCLibTestDlg.h"
#include "afxdialogex.h"
#include "Calculator.h"```

```void CMFCLibTestDlg::OnBnClickedButtonCal() {
UpdateData(TRUE);
CString strTemp;
double numbers[2];
numbers[0] = m_Num1;
numbers[1] = m_Num2;

strTemp.Format(L"%.2f", Max(numbers,2));
m_strText.Append(L"Max is:\t" + strTemp);

strTemp.Format(L"%.2f", Min(numbers, 2));
m_strText.Append(L"\nMin is:\t" + strTemp);

strTemp.Format(L"%.2f", Sum(numbers, 2));
m_strText.Append(L"\nSum is:\t" + strTemp);

strTemp.Format(L"%.2f", Average(numbers, 2));
m_strText.Append(L"\nAverage is:\t" + strTemp);

strTemp.Format(L"%d", GreatestCommonDivisor(m_Num1, m_Num2));
m_strText.Append(L"\nGDC is:\t" + strTemp);

UpdateData(FALSE);
}```

## 动态库

Win32 DLL是可用于Microsoft Windows计算机上运行的程序的库，作为普通库，它由分组在文件中的功能/其他资源组成。

DLL的缩写代表动态链接库，这意味着，与静态库相反，DLL使程序员可以决定何时以及如何将其他应用程序链接到这种类型的库。

`extern "C" _declspec(dllexport)`

```//MFCDynamicLib.cpp : Defines the exported functions for the DLL application.//

#include "stdafx.h"

extern "C" _declspec(dllexport) double Min(const double *Numbers, const int Count);
extern "C" _declspec(dllexport) double Max(const double *Numbers, const int Count);
extern "C" _declspec(dllexport) double Sum(const double *Numbers, const int Count);
extern "C" _declspec(dllexport) double Average(const double *Numbers, const int Count);
extern "C" _declspec(dllexport) long GreatestCommonDivisor(long Nbr1, long Nbr2);

double Min(const double *Nbr, const int Total) {
double Minimum = Nbr[0];
for (int i = 0; i < Total; i++)
if (Minimum > Nbr[i])
Minimum = Nbr[i];
return Minimum;
}
double Max(const double *Nbr, const int Total) {
double Maximum = Nbr[0];
for (int i = 0; i < Total; i++)
if (Maximum < Nbr[i])
Maximum = Nbr[i];
return Maximum;
}
double Sum(const double *Nbr, const int Total) {
double S = 0;
for (int i = 0; i < Total; i++)
S += Nbr[i];
return S;
}
double Average(const double *Nbr, const int Total){
double avg, S = 0;
for (int i = 0; i < Total; i++)
S += Nbr[i];
avg = S/Total;
return avg;
}
long GreatestCommonDivisor(long Nbr1, long Nbr2) {
while (true) {
Nbr1 = Nbr1 % Nbr2;
if (Nbr1 == 0)
return Nbr2;
Nbr2 = Nbr2 % Nbr1;
if (Nbr2 == 0)
return Nbr1;
}
}```

```extern "C" _declspec(dllimport) double Min(const double *Numbers, const int Count);
extern "C" _declspec(dllimport) double Max(const double *Numbers, const int Count);
extern "C" _declspec(dllimport) double Sum(const double *Numbers, const int Count);
extern "C" _declspec(dllimport) double Average(const double *Numbers, const int Count);
extern "C" _declspec(dllimport) long GreatestCommonDivisor(long Nbr1, long Nbr2);```

```void CMFCLibTestDlg::OnBnClickedButtonCal() {

UpdateData(TRUE);

CString strTemp;
double numbers[2];
numbers[0] = m_Num1;
numbers[1] = m_Num2;

strTemp.Format(L"%.2f", Max(numbers,2));
m_strText.Append(L"Max is:\t" + strTemp);

strTemp.Format(L"%.2f", Min(numbers, 2));
m_strText.Append(L"\nMin is:\t" + strTemp);

strTemp.Format(L"%.2f", Sum(numbers, 2));
m_strText.Append(L"\nSum is:\t" + strTemp);

strTemp.Format(L"%.2f", Average(numbers, 2));
m_strText.Append(L"\nAverage is:\t" + strTemp);

strTemp.Format(L"%d", GreatestCommonDivisor(m_Num1, m_Num2));
m_strText.Append(L"\nGDC is:\t" + strTemp);

UpdateData(FALSE);
}```