创建和使用动态链接库、静态链接库 (C++)

参考:

1、https://msdn.microsoft.com/zh-cn/library/ms235636.aspx  演练:创建和使用动态链接库 (C++)

2、https://msdn.microsoft.com/zh-cn/library/ms235627.aspx  演练:创建和使用静态库 (C++)

摘要:

一、创建和使用动态链接库

 1 #ifdef MATHFUNCSDLL_EXPORTS
 2 #define MATHFUNCSDLL_API __declspec(dllexport) 
 3 #else
 4 #define MATHFUNCSDLL_API __declspec(dllimport) 
 5 #endif
 6 
 7 namespace MathFuncs
 8 {
 9     // This class is exported from the MathFuncsDll.dll
10     class MyMathFuncs
11     {
12     public:
13         // Returns a + b
14         static MATHFUNCSDLL_API double Add(double a, double b);
15 
16         // Returns a - b
17         static MATHFUNCSDLL_API double Subtract(double a, double b);
18 
19         // Returns a * b
20         static MATHFUNCSDLL_API double Multiply(double a, double b);
21 
22         // Returns a / b
23         // Throws const std::invalid_argument& if b is 0
24         static MATHFUNCSDLL_API double Divide(double a, double b);
25     };
26 }
MathFuncsDll.h
 1 // 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++)

 1 // MathFuncsLib.h
 2 
 3 namespace MathFuncs
 4 {
 5     class MyMathFuncs
 6     {
 7     public:
 8         // Returns a + b
 9         static double Add(double a, double b);
10 
11         // Returns a - b
12         static double Subtract(double a, double b);
13 
14         // Returns a * b
15         static double Multiply(double a, double b);
16 
17         // Returns a / b
18         static double Divide(double a, double b);
19     };
20 }
MathFuncsLib.h
 1 // MathFuncsLib.cpp
 2 // compile with: cl /c /EHsc MathFuncsLib.cpp
 3 // post-build command: lib MathFuncsLib.obj
 4 
 5 #include "MathFuncsLib.h"
 6 
 7 #include <stdexcept>
 8 
 9 using namespace std;
10 
11 namespace MathFuncs
12 {
13     double MyMathFuncs::Add(double a, double b)
14     {
15         return a + b;
16     }
17 
18     double MyMathFuncs::Subtract(double a, double b)
19     {
20         return a - b;
21     }
22 
23     double MyMathFuncs::Multiply(double a, double b)
24     {
25         return a * b;
26     }
27 
28     double MyMathFuncs::Divide(double a, double b)
29     {
30         return a / b;
31     }
32 }
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"。

 

posted @ 2017-03-30 09:27  零基础刷书博客  阅读(432)  评论(0编辑  收藏  举报