Dynamic library learning Chapter 1-Drills: creating and using dynamic link libraries (C ++)

Source: Internet
Author: User

Walkthrough: create and use a dynamic link library(C ++)

 

Visual Studio 2008

  • · Visual maxcompute 2005

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.

Required

This topic assumes that you have basic knowledge of the C ++ language. If you are just learning C ++, we recommend that you refer to the "C ++ ininner's Guide" ("C ++ beginner's Guide") written by Herb schildt. the book is available from http://go.microsoft.com/fwlink? Linkid = 115303 online.

Create a new dynamic link library(DLL)Project

  1. Slave"File"In the menu, select"New"And then select"Project...".
  2. In"Project Type"In the pane, select"Visual c ++"Under"Win32".
  3. In"Template"In the pane, select"Win32Console Application".
  4. Select a name for the project, such as mathfuncsdll, and enter"Name"Field. Select a name for the solution, such as dynamiclibrary, and enter it"Solution name"Field.
  5. Click"OK"Start the Win32 Application Wizard. In"Win32Application Wizard"Dialog Box"Overview"Page, click"Next step".
  6. In"Win32Application Wizard"In"Application settings"Page, select"Application Type"Under"DLL"(If available), or select"Console Application"(If"DLL"Unavailable ). 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.
  7. In"Win32Application Wizard"Of"Application settings"Page, select"Additional options"Under"Empty Project".
  8. Click"Complete"Create a project.

Add a class to the dynamic link library

  1. To create a header file for the new class"Project"Select"Add new item...". Will display"Add new item"Dialog box. In"Category"In the pane, select"Visual c ++"Under"Code". In"Template"Select"Header file(. H )". Select a name for the header file, such as mathfuncsdll. H, and click"Add". A blank file is displayed.
  2. 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

// 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 );

};

}

  1. Note that_ Declspec (dllexport)Modifier. These modifiers enable the DLL to export this method for use by other applications. For more information, see dllexport and dllimport.
  2. To create a source file for the new class"Project"Select"Add new item...". Will display"Add new item"Dialog box. In"Category"In the pane, select"Visual c ++"Under"Code". In"Template"In the pane, select"C ++File(. Cpp )". Select a name for the source file, such as mathfuncsdll. cpp, and click"Add". A blank file is displayed.
  3. "Mymathfuncs" is implemented in the source file. The Code should be similar to the following:

Copy

// Mathfuncsdll. cpp

// Compile with:/ehs/ LD

 

# Include "mathfuncsdll. H"

 

# Include <stdexcept>

 

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;

}

}

  1. To generate a project as a DLL"Project"Select mathfuncsdll from the menu."Attribute...". In the left pane, select"Configure attributes"Under"General". In the right pane, Set"Configuration type"Change"Dynamic library(. Dll )". Click"OK"Save the changes.

Note:

If you generate a project from the command line, use/LDThe compiler option specifies that the output file should be DLL. For more information, see/MD,/mt,/LD (use the Runtime Library ).

  1. Compile the dynamic link library by selecting"Generate"In the menu"Generate a solution". 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

  1. To create an application that will reference and use the newly created dynamic link library"File"Select"New"And then select"Project...".
  2. In"Project Type"In the pane, select"Visual c ++"Under"Win32".
  3. In"Template"In the pane, select"Win32Console Application".
  4. Select a name for the project (for example, myexecrefsdll) and enter"Name"Field. Slave"Solution"Select"Add Solution". This will add the new project to the same solution to which the dynamic link library belongs.
  5. Click"OK"Start"Win32Application Wizard". In"Win32Application Wizard"Dialog Box"Overview"Page, click"Next step".
  6. In"Win32Application Wizard"Of"Application settings"Page, select"Application Type"Under"Console Application".
  7. In"Win32Application Wizard"Of"Application settings"Page, clear"Additional options"Under"Precompiled header"Check box.
  8. Press"Complete"Create a project.

Use the class library function in the console application

  1. 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 ".
  2. To use the arithmetic routines created in the dynamic link library, you must reference the library. To perform this operation, select the myexecrefsdll project in Solution Explorer and"Project"Select"Reference...". In"Property page"Dialog box, expand"Common attributes"Node, select"Framework and references"And then select"Add new reference..."Button. Related"Reference..."For more information about the dialog box, see <projectname> properties page Dialog Box> general attributes> framework and reference ".
  3. Display"Add reference"Dialog box. This dialog box lists all databases that can be referenced."Project"The tab lists all projects in the current solution and all libraries they contain. In"Project"Tab, and select mathfuncsdll. Click"OK".
  4. To reference the header file of the dynamic link library, you must modify the directory path. For this reason, go"Property page"Expand in the dialog box"Configure attributes"And expand"C/C ++"Node, and select"General". In"Add a directory"Next to, type the path where the mathfuncsdll. h header file is located.
  5. The executable file only loads the dynamic link library at runtime. You must tell the system where to find "mathfuncsdll. dll ". You can usePathEnvironment variables do this. For this reason, go"Property page"Expand in the dialog box"Configure attributes"Node, and select"Debugging". In"Environment"Enter the following content: Path = <path of the mathfuncsdll. dll File>, where<Mathfuncsdll. dllFile Path> replace it with the actual location of mathfuncsdll. dll. Click"OK"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 file at the command prompt.PathEnvironment variables: Set Path = % PATH %; <path of the mathfuncsdll. dll File>, where <Mathfuncsdll. dllFile Path> replace it with the actual location of mathfuncsdll. dll.

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

Copy

// Myexecrefsdll. cpp

// Compile with:/ehs/ link mathfuncsdll. Lib

 

# Include <iostream>

 

# 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;

}

  1. From"Generate"Select"Generate a solution"To generate an executable file.

Run the application

  1. Make sure that "myexecrefsdll" is selected as the default project. In"Solution Resource Manager", Select myexecrefsdll, and then select"Project"In the menu"Set as startup project".
  2. To run the project, select"Debugging"In the menu"Start execution (not debugging)". The output should be similar to the following content:

Copy

A + B = 106.4

A-B =-91.6

A * B = 732.6

A/B = 0.0747475

 

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.