"Reprint" Create and use dynamic link libraries (c + +)

Source: Internet
Author: User

Original: http://blog.csdn.net/handforcpp/article/details/3478254

You can also refer to MSDN:

Walkthrough: Creating and Using a dynamic-link library (c + +)

The first type of library we will create is a dynamic-link library (DLL). Using DLLs is a great way to reuse code. You do not have to re-implement the same routine in every program you create, but simply write them once and then reference them from the application that needs the functionality.

This walkthrough covers the following topics:

Creates a new dynamic-link library (DLL) project.

Add a class to the dynamic-link library.

Create an application that references a dynamic-link library.

The ability to use the class library in a console application.

Run the application.

Prerequisite
This topic assumes that you have the basic knowledge of the C + + language.

To create a new dynamic-link library (DLL) project
From the File menu, choose New, and then select Project ....

In the Project Types pane, select Win32 under Visual C + +.

In the Templates pane, select Win32 Console Application.

Select a name for the project, such as "MathFuncsDll", and type it in the Name field. Select a name for the solution, such as "dynamiclibrary", and type it into the Solution Name field.

Click OK to start the Win32 Application Wizard. In the Win32 Application Wizard dialog box, on the Overview page, click Next.

In the Application Settings page of the WIN32 Application Wizard, select DLL under Application type (if available), or console application (if DLL is not available). Some versions of Visual Studio do not support creating DLL projects by using wizards. You can make changes to this later to compile the project as a DLL.

In the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty Project.

Click Finish to create the project.

Add a class to a dynamic-link library
To create a header file for your new class, choose Add New Item ... from the Project menu. The Add New Item dialog box is displayed. In the Categories pane, select Code under Visual C + +. Select Header file (. h) in the Templates pane. Select a name for the header file, such as "MathFuncsDll.h", and click Add. A blank file is displayed.

Add a simple class named "Mymathfuncs" to perform common arithmetic operations such as add, subtract, multiply, and divide. The code should resemble the following:

Copy Code

//MathFuncsDll.hnamespaceMathfuncs {classMymathfuncs { Public: //Returns A + bStatic__declspec (dllexport)DoubleADD (DoubleADoubleb); //Returns A-BStatic__declspec (dllexport)DoubleSubtract (DoubleADoubleb); //Returns A * bStatic__declspec (dllexport)DoubleMultiply (DoubleADoubleb); //Returns A/b//Throws DivideByZeroException If B is 0Static__declspec (dllexport)DoubleDivide (DoubleADoubleb); }; } 



Note the __declspec (dllexport) modifier in this code method declaration. These modifiers enable the DLL to export the method for use by other applications. For more information, see dllexport, DllImport.

To create a source file for a new class, from the Project menu, choose Add New Item .... The Add New Item dialog box is displayed. In the Categories pane, select Code under Visual C + +. In the Templates pane, select C + + file (. cpp). Select a name for the source file, such as MathFuncsDll.cpp, and click Add. A blank file is displayed.

The ability to implement "Mymathfuncs" in the source file. The code should resemble the following:

Copy Code

//MathFuncsDll.cpp//compile with:/ehsc/ld#include"MathFuncsDll.h"#includeusing namespacestd;namespaceMathfuncs {DoubleMymathfuncs::add (DoubleADoubleb) {returnA +b;} DoubleMymathfuncs::subtract (DoubleADoubleb) {returnAb;} DoubleMymathfuncs::multiply (DoubleADoubleb) {returnAb;} DoubleMymathfuncs::D ivide (DoubleADoubleb) {if(b = =0) { Throw NewInvalid_argument ("b cannot be zero!"); } returnAb;} } 



To make a project a DLL, choose MathFuncsDll from the Project menu, and then select Properties .... In the left pane, under Configuration Properties, select General. In the right pane, change the configuration type to dynamic library (. dll). Click OK to save the changes.

Description
If you build the project from the command line, use the/LD compiler option to specify that the output file should be a DLL. For more information, see/MD,/MT,/LD (using the run-time library).


Compile the dynamic-link library by selecting Build Solution from the Build menu. This creates a DLL that can be used by other programs. For more information about DLLs, see DLLs.

To create an application that references a dynamic-link library
To create an application that will reference and use the dynamic-link library you just created, choose New from the File menu, and then select Project ....

In the Project Types pane, select Win32 under Visual C + +.

In the Templates pane, select Win32 Console Application.

Select a name for the project (such as "MyExecRefsDll") and type it into the Name field. Select Add to solution from the drop-down list next to solution. This adds the new project to the same solution that the dynamic link library belongs to.

Click OK to start the Win32 Application Wizard. In the Win32 Application Wizard dialog box, on the Overview page, click Next.

In the Application Settings page of the WIN32 Application Wizard, select Console application under Application type.

In the Application Settings page of the Win32 Application Wizard, clear the Precompiled Header check box under Additional options.

Press Finish to create the project.

The ability to use class libraries in a console application
After you create a new console application, an empty program is created for you. The name of the source file is the same as the name you selected earlier for the project. In this example, the name is "MyExecRefsDll.cpp".

To use an arithmetic routine created in a dynamic-link library, you must reference the library. To do this, select Reference ... from the Project menu. In the Property Pages dialog box, expand the Common Properties node, select References, and then select the Add New Reference ... button. For more information about the Reference ... dialog box, see Framework and references, common Properties, Property Pages dialog box.

Displays the Add Reference dialog box. This dialog box lists all the libraries that can be referenced. The Projects tab lists all the projects in the current solution, along with all the libraries they contain. In the Projects tab, select MathFuncsDll. then click OK. For more information about the Add Reference dialog box, see Add Reference dialog box.

To reference the header file of a dynamic-link library, you must modify the Include directory path. To do this, in the Property Pages dialog box, expand the Configuration Properties node, and then expand the C + + node, and select General. Next to additional Include directories, type the path where the MathFuncsDll.h header file is located.

The executable file loads the dynamic-link library only at run time. You must tell the system where to look for "MathFuncsDll.dll". You can do this by using the PATH environment variable. To do this, in the Property Pages dialog box, expand the Configuration Properties node, and select Debug. Next to environment, type the following: Path=, which should be replaced with the actual location of "MathFuncsDll.dll". Click OK to save all the changes.

Description
If you want to run the executable from the command line instead of from Visual Studio, you must manually update the PATH environment variable from the command prompt, as follows: Set path=%path%, which should be replaced with the actual location of "MathFuncsDll.dll".


Now, you can use the "Mymathfuncs" class in your application. Replace the contents of "MyExecRefsDll.cpp" with the following code:

Copy Code

//MyExecRefsDll.cpp//compile with:/ehsc/link MathFuncsDll.lib#include #include"MathFuncsDll.h" using namespacestd;intMain () {DoubleA =7.4; intb = About; cout<<"A + b ="<<Mathfuncs::mymathfuncs::add (A, b)<<Endl; cout<<"A-B ="<<Mathfuncs::mymathfuncs::subtract (A, b)<<Endl; cout<<"A * b ="<<Mathfuncs::mymathfuncs::multiply (A, b)<<Endl; cout<<"A/ B ="<<Mathfuncs::mymathfuncs::D ivide (A, b)<<Endl;return 0; } 



Build the executable file by choosing Build Solution from the Build menu.

Running the application
Make sure that "MyExecRefsDll" is selected as the default item. In Solution Explorer, select MyExecRefsDll, and then choose Set as Startup Project on the Project menu.

To run the project, choose Start Without Debugging on the Debug menu. The output should look similar to the following:

Copy Code
A + b = 106.4
A-B =-91.6
A * b = 732.6
A/b = 0.0747475

In fact, to build an empty project, in the property page set on the line, and the original is a bit annoying, using the generated events, I think it is easy to complete the assignment of DLLs.

"Reprint" Create and use dynamic link libraries (c + +)

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.