Create and use dynamic link libraries, static link libraries (C++)
Keywords:
C++
Windows
Reference resources:
1,https://msdn.microsoft.com/zh-cn/library/ms235636.aspx Walkthrough: Creating and using dynamic link libraries (C++)
2,https://msdn.microsoft.com/zh-cn/library/ms235627.aspx Walkthrough: Create and use static libraries (C++)
Summary:
1. Creating and using dynamic link libraries
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
By default, the DLL's New Project template adds PROJECTNAME_EXPORTS to the defined symbols of the DLL project.The MATHFUNCSDLL_API symbol sets the u declspec(dllexport) modifier in the member function declaration of this code.If other programs include header files, the MATHFUNCSDLL_API defines the u declspec(dllimport) modifier in the member function declaration.
The Add Reference dialog box lists the libraries that can be referenced.The Project tab lists all projects in the current solution and all libraries they contain.On the Project tab, select the check box next to MathFuncsDll and select the OK button.
To reference the header file of a DLL, you must modify the included directory path.To do this, in the Properties Page dialog box, expand the Configure Properties node and the C/C++ node in turn, and then select General.Next to Additional Include Directory, specify the location path of the MathFuncsDll.h header file.You can use a relative path (for example. \MathFuncsDll\) and select the OK button.
2. Create and use static libraries (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
Add references and include directory paths consistent with dynamic link libraries
3. Precompiled Header Files
Header file precompilation refers to the precompilation of some MFC standard header files (such as Windows.H, Afxwin.H) used in a project. When the project is compiled later, this part of the header file is no longer compiled and only the precompiled results are used.This will speed up the compilation and save time.The precompiled header file is generated by compiling stdafx.cpp and named after the project. Since the precompiled header file is suffixed with "pch", the compiled result file is projectname.pch.The compiler uses a precompiled header file, stdafx.h.Stdafx.h is a header file name that can be specified in the compilation settings of the project.The compiler considers that all code precompiled before the directive #include "stdafx.h", skipping the #include "stdafx.h" directive and compiling all code after the directive using projectname.pch.(At compile time, statements preceding #include "stdafx. h" were not compiled). Therefore, the first statement in all CPP implementation files is: #include "stdafx.h".