参考:
1、https://msdn.microsoft.com/zh-cn/library/ms235636.aspx 演练:创建和使用动态链接库 (C++)
2、https://msdn.microsoft.com/zh-cn/library/ms235627.aspx 演练:创建和使用静态库 (C++)
摘要:
一、创建和使用动态链接库
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public:
// Returns a + b
static MATHFUNCSDLL_API double Add(double a, double b);
// Returns a - b
static MATHFUNCSDLL_API double Subtract(double a, double b);
// Returns a * b
static MATHFUNCSDLL_API double Multiply(double a, double b);
// Returns a / b
// Throws const std::invalid_argument& if b is 0
static MATHFUNCSDLL_API double Divide(double a, double b);
};
} MathFuncsDll.h1 // MathFuncsDll.cpp : Defines the exported functions for the DLL application.
2 //
3
4 #include "stdafx.h"
5 #include "MathFuncsDll.h"
6 #include <stdexcept>
7 using namespace std;
8
9 namespace MathFuncs
10 {
11 double MyMathFuncs::Add(double a, double b)
12 {
13 return a + b;
14 }
15
16 double MyMathFuncs::Subtract(double a, double b)
17 {
18 return a - b;
19 }
20
21 double MyMathFuncs::Multiply(double a, double b)
22 {
23 return a * b;
24 }
25
26 double MyMathFuncs::Divide(double a, double b)
27 {
28 if (b == 0)
29 {
30 throw invalid_argument("b cannot be zero!");
31 }
32
33 return a / b;
34 }
35 } MathFuncsDll.cpp默认情况下,DLL 的“新建项目”模板会将 PROJECTNAME_EXPORTS 添加到 DLL 项目的已定义符号中。MATHFUNCSDLL_API 符号将在此代码的成员函数声明中设置 __declspec(dllexport) 修饰符。若其他程序包含了头文件,则 MATHFUNCSDLL_API 将定义成员函数声明中的 __declspec(dllimport) 修饰符。

“添加引用”对话框列出了可以引用的库。 “项目”选项卡列出了当前解决方案中的所有项目以及它们包含的所有库。 在“项目”选项卡上,选中“MathFuncsDll”旁边的复选框,然后选择“确定”按钮。
若要引用 DLL 的头文件,必须修改包含的目录路径。 若要执行此操作,请在“属性页”对话框中,依次展开“配置属性”节点和“C/C++”节点,然后选择“常规”。在“附加包含目录”旁边,指定 MathFuncsDll.h 头文件的位置路径。 你可以使用相对路径(例如 ..\MathFuncsDll\),然后选择“确定”按钮。
二、创建和使用静态库 (C++)

// MathFuncsLib.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
static double Divide(double a, double b);
};
} MathFuncsLib.h// MathFuncsLib.cpp
// compile with: cl /c /EHsc MathFuncsLib.cpp
// post-build command: lib MathFuncsLib.obj
#include "MathFuncsLib.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
return a / b;
}
} MathFuncsLib.cpp
添加引用及添加包含目录路径与动态链接库一致
三、预编译头文件
所谓头文件预编译,就是把一个工程(Project)中使用的一些MFC标准头文件(如Windows.H、Afxwin.H)预先编译,以后该工程编译时,不再编译这部分头文件,仅仅使用预编译的结果。这样可以加快编译速度,节省时间。预编译头文件通过编译stdafx.cpp生成,以工程名命名,由于预编译的头文件的后缀是“pch”,所以编译结果文件是projectname.pch。编译器通过一个头文件stdafx.h来使用预编译头文件。stdafx.h这个头文件名是可以在project的编译设置里指定的。编译器认为,所有在指令#include "stdafx.h"前的代码都是预编译的,它跳过#include "stdafx. h"指令,使用projectname.pch编译这条指令之后的所有代码。(在编译的时候,在#include "stdafx. h"前面的语句都不予以编译).因此,所有的CPP实现文件第一条语句都是:#include "stdafx.h"。