Create and use dynamic link library, static link library (C ++), dynamic link library static

Source: Internet
Author: User

Create and use dynamic link library, static link library (C ++), dynamic link library static

Refer:

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: creating and using static libraries (C ++)

Abstract:

1. Create and use 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.dll10     class MyMathFuncs11     {12     public:13         // Returns a + b14         static MATHFUNCSDLL_API double Add(double a, double b);15 16         // Returns a - b17         static MATHFUNCSDLL_API double Subtract(double a, double b);18 19         // Returns a * b20         static MATHFUNCSDLL_API double Multiply(double a, double b);21 22         // Returns a / b23         // Throws const std::invalid_argument& if b is 024         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 MathFuncs10 {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 "new project" template of DLL adds PROJECTNAME_EXPORTS to the defined symbols of the DLL project. The MATHFUNCSDLL_API symbol will be set in the member function declaration of this Code__declspec(dllexport)Modifier. If other programs contain header files, MATHFUNCSDLL_API will define__declspec(dllimport)Modifier.

"Add reference"The dialog box lists the databases that can be referenced."Project"The tab lists all projects in the current solution and all libraries they contain. In"Project"On the "MathFuncsDll" tab, select the check box next to "MathFuncsDll", and then select"OK"Button.

To reference the DLL header file, you must modify the directory path. To perform this operation, go"Property page"In the dialog box, expand"Configuration attributes"Nodes and"C/C ++"Node, and then select"Regular". In"Add include directory"Specify the path of the MathFuncsDll. h header file. You can use the relative path (for example, .. \ MathFuncsDll \), and then select"OK"Button.

Ii. Create and use a static library (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 - b12         static double Subtract(double a, double b);13 14         // Returns a * b15         static double Multiply(double a, double b);16 17         // Returns a / b18         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 MathFuncs12 {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 reference and add directory paths consistent with Dynamic Link Library

Iii. Pre-compiled header files

Header file pre-compilation refers to some of the MFC standard header files used in a Project (such as Windows. h. Afxwin. h) pre-compilation. Later, this header file will not be compiled and only the pre-compilation results will be used. This will speed up compilation and save time. The precompiled header file is generated by compiling stdafx. cpp and named after the project name. Because the precompiled header file is suffixed with "pch", the compilation result file is projectname. pch. The compiler uses the pre-compiled header file through a header file stdafx. h. The stdafx. h header file name can be specified in the project compilation settings. The compiler considers that all commands in the # include "stdafx. h "the previous code is pre-compiled, and it skips # include" stdafx. h "command, using projectname. pch compiles all the code after this command. (During compilation, in # include "stdafx. h "the preceding statements are not compiled ). therefore, the first statement of all CPP implementation files is: # include "stdafx. h ".

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.