Create and use dynamic link library (C ++)

Source: Internet
Author: User

The first type of library we will create is the dynamic link library (DLL ). Using DLL is an excellent way to reuse code. You don't have to re-implement the same routine in every program you create, but you just need to write these routines once and then reference them from the application that requires this function.

This drill covers the following:

Create a new dynamic link library (DLL) project.

Add a class to the dynamic link library.

Create an application that references the dynamic link library.

Use the class library function in the console application.

Run the application.

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

Create a new dynamic link library (DLL) project
Select "new" from the "file" menu, and then select "project ...".

In the project type pane, select Win32 under Visual C ++ ".

In the template pane, select Win32 console application ".

Select a name for the project, such as mathfuncsdll, and enter the name field. Select a name for the solution, such as "dynamiclibrary", and enter the "solution name" field.

Click OK to start the Win32 Application Wizard. On the overview page of the Win32 Application Wizard dialog box, click Next ".

On the "application settings" page in the "Win32 Application Wizard", select "DLL" under "application type" (if available ), or select "console application" (if "DLL" is not available ). Some versions of Visual Studio do not support creating DLL projects by using the wizard. You can make changes later to compile the project as a DLL.

On the "application settings" page of the "Win32 Application Wizard", select "Empty Project" under "additional options ".

Click "finish" to create a project.

Add a class to the dynamic link library
To create a header file for the new class, select "Add new project..." from the "project" menu ...". The "Add new item" dialog box is displayed. In the category pane, select code under Visual C ++ ". In the template pane, select header file (. h )". 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 addition, subtraction, multiplication, and division. The Code should be similar to the following:

Copy code
// Mathfuncsdll. h

Namespace mathfuncs
{
Class mymathfuncs
{
Public:
// Returns a + B
Static _ declspec (dllexport) Double add (double A, double B );

// Returns a-B
Static _ declspec (dllexport) Double subtract (double A, double B );

// Returns a * B
Static _ declspec (dllexport) Double multiply (double A, double B );

// Returns a/B
// Throws dividebyzeroexception if B is 0
Static _ declspec (dllexport) Double divide (double A, double B );
};
}

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

To create a source file for the new class, select "Add new project..." from the "project" menu ...". The "Add new item" dialog box is displayed. In the category pane, select code under Visual C ++ ". In the template pane, select C ++ file (. cpp )". Select a name for the source file, such as mathfuncsdll. cpp, and click Add ". A blank file is displayed.

"Mymathfuncs" is implemented in the source file. The Code should be similar to the following:

Copy code
// Mathfuncsdll. cpp
// Compile with:/ehs/ LD

# Include "mathfuncsdll. H"

# Include

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)
{
If (B = 0)
{
Throw new invalid_argument ("B cannot be zero! ");
}

Return A/B;
}
}

To generate a project as a DLL, select "mathfuncsdll" from the "project" menu, and then select "properties ...". In the left pane, select general under "configuration properties ". In the right pane, change "configuration type" to "dynamic library (. dll )". Click OK to save the changes.

Note:
If you generate a 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 (use the Runtime Library ).

Compile the dynamic link library by selecting "generate solution" in the "generate" menu ". In this way, a DLL is created for other programs. For more information about DLL, see DLL.

Create an application that references the dynamic link library
To create an application that will reference and use the created dynamic link library, select "new" from the "file" menu and select "project ...".

In the project type pane, select Win32 under Visual C ++ ".

In the template pane, select Win32 console application ".

Select a name for the project (for example, "myexecrefsdll") and enter the "name" field. Select "add solution" from the drop-down list next to "solution ". This will add the new project to the same solution to which the dynamic link library belongs.

Click OK to start the Win32 Application Wizard ". On the overview page of the Win32 Application Wizard dialog box, click Next ".

On the "application settings" page of the "Win32 Application Wizard", select "console application" under "application type ".

On the application settings page of the Win32 Application Wizard, clear the pre-compiled header check box under additional options.

Click "finish" to create a project.

Use the class library function in the console application
After creating a new console application, an empty program will be created for you. The source file name is the same as the name you selected for the project. In this example, the name is "myexecrefsdll. cpp ".

To use the arithmetic routines created in the dynamic link library, you must reference the library. To do this, select "reference…" in the "project" menu ...". In the "property page" dialog box, expand the "general attributes" node, select "Reference", and then select "Add new reference. For more information about the "reference..." dialog box, see" Property page dialog box-> general attributes-> framework and reference ".

The "add reference" dialog box is displayed. This dialog box lists all databases that can be referenced. The projects tab lists all the projects in the current solution and all the libraries they contain. On the projects tab, select mathfuncsdll ". Click OK ". For more information about the "add reference" dialog box, see the "add reference" dialog box.

To reference the header file of the dynamic link library, you must modify the directory path. To do this, expand the "configuration properties" node in the "properties page" dialog box, expand the "C/C ++" node, and select "General ". Next to "add include directory", type the path where the header file "mathfuncsdll. H" is located.

The executable file only loads the dynamic link library at runtime. You must tell the system where to find "mathfuncsdll. dll ". You can do this by using the PATH environment variable. To do this, expand the "configuration properties" node in the "property page" dialog box and select "debug ". Enter the following content next to "environment": Path =
, Where
Replace it with the actual location of "mathfuncsdll. dll. Click OK to save all changes.

Note:
If you want to run the executable file from the command line instead of Visual Studio, you must manually update the PATH environment variable from the command prompt, as shown in the following figure: Set Path = % PATH %;
, Where
Replace it with the actual location of "mathfuncsdll. dll.

Now, you can use the "mymathfuncs" class in the application. Use the following code to replace "myexecrefsdll. cpp:

Copy code
// Myexecrefsdll. cpp
// Compile with:/ehs/ link mathfuncsdll. Lib

# Include

# Include "mathfuncsdll. H"

Using namespace STD;

Int main ()
{
Double A = 7.4;
Int B = 99;

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: Divide (a, B) <Endl;

Return 0;
}

Select "generate solution" from the "generate" menu to generate an executable file.

Run the application
Make sure that "myexecrefsdll" is selected as the default project. In Solution Explorer, select "myexecrefsdll" and select "set as startup project" in the "project" menu ".

To run the project, select "start execution (not debugging)" in the "debug" menu )". The output should be similar to the following content:

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

In fact, it is enough to set up an empty project on the property page, and the original article is a bit annoying. I think it is easy to assign values to DLL by generating events.

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.