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.
System Prerequisites
This topic assumes that you have the basic knowledge of the C + + language. If you are just beginning to learn C + +, we recommend that you refer to "C + + Beginner's Guide" written by Herb Schildt ("C + + Beginner's Guides"), which can be found from http://go.microsoft.com/fwlink/? Linkid=115303 is available online.
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 in 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:
Mathfuncsdll.hnamespace 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); 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 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:
mathfuncsdll.cpp//compile with:/ehsc/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-a ; } Double mymathfuncs::multiply (double A, double b) { return a * b; } Double Mymathfuncs::D ivide (Double A, double b) { if (b = = 0) { throw new invalid_argument ("B cannot b e zero! "); } return a/b; }}
To make the project a DLL, choose MathFuncsDll Properties ... from the Project menu. 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.
Attention
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 runtime).
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 in 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 the MyExecRefsDll project in Solution Explorer, and then select Reference ... from the Project menu. In the Property Pages dialog box, expand the Common Properties node, select frames and references, and then select the Add New Reference ... button. For more information about the Reference ... dialog box, see Framework and references, common Properties, <Projectname> 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.
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: The path to the Path=<mathfuncsdll.dll file, where the path > of the <mathfuncsdll.dll file should be replaced with the actual location of MathFuncsDll.dll. Click OK to save all the changes.
Attention
If you want to run the executable from the command line rather than from Visual Studio, you must manually update the path environment variable at a command prompt, as follows: The path to the set Path=%path%;<mathfuncsdll.dll file Where the <mathfuncsdll.dll file's path > 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:
myexecrefsdll.cpp//compile with:/ehsc/link mathfuncsdll.lib#include <iostream> #include "MathFuncsDll.h" Using namespace Std;int Main () { double A = 7.4; int b =; cout << "A + b =" << mathfuncs::mymathfuncs::add (A, b) << Endl; cout << " A-B =" << mathfuncs::mymathfuncs::subtract (A, c) << 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:
A + b = 106.4
A-B =-91.6
A * b = 732.6
A/b = 0.0747475
Http://www.cnblogs.com/rollenholt/archive/2012/04/14/2447017.html
Walkthrough: Creating and Using a dynamic-link library (c + +)