Walkthrough: create and use a 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 a type of reuseCodeExcellent way. You do not have to createProgramYou only need to write these routines once and then reference them from the application that needs the 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. Select "new" from the "file" menu, and then select "project ...".

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

    3. In the template pane, select Win32 console application ".

    4. 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.

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

    6. 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.

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

    8. Click "finish" to create a project.

Add a class to the dynamic link library
    1. 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.

    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:

 

?
123456789101112131415161718192021 // 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 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.

    2. 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.

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

 

?
123456789101112131415161718192021222324252627282930313233343536 // 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, select mathfuncsdll "property..." from the "project" menu ...". 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/LDThe compiler option specifies that the output file should be DLL. For more information, see/MD,/mt,/LD (use the Runtime Library ).

    2. 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
    1. To create an application that will reference and use the created dynamic link library, select "new" from the "file" menu and select "project ...".

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

    3. In the template pane, select Win32 console application ".

    4. Select a name (for example, myexecrefsdll) for the project 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.

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

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

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

    8. Click "finish" to 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 select reference... from the project menu ...". In the "property page" dialog box, expand the "general attributes" node, select "framework and reference", and then select "Add new reference. For more information about the "reference..." dialog box, see "<projectname> properties page" dialog box> "general attributes"> "framework and reference ".

  3. 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 ".

  4. 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 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. To do this, expand the "configuration properties" node in the "property page" dialog box and select "debug ". Enter Path = <path of the mathfuncsdll. dll File> next to "Environment". Replace <path of the mathfuncsdll. dll File> 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 file at the command prompt.PathEnvironment variables: Set Path = % PATH %; <mathfuncsdll. DLL file path>, where <mathfuncsdll. DLL file path> should be replaced with mathfuncsdll. the actual location of the DLL.

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

 

?
12345678910111213141516171819202122232425 // 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. Select "generate solution" from the "generate" menu to generate an executable file.

Run the application
    1. 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 ".

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

    3. A + B = 106.4
      A-B =-91.6
      A * B = 732.6
      A/B = 0.0747475

 

From: http://www.cnblogs.com/rollenholt/archive/2012/04/14/2447017.html

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.