Walkthrough: create and use a dynamic link library(C ++)
Visual Studio 2008
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
- Slave"File"In the menu, select"New"And then select"Project...".
- In"Project Type"In the pane, select"Visual c ++"Under"Win32".
- In"Template"In the pane, select"Win32Console Application".
- 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.
- Click"OK"Start the Win32 Application Wizard. In"Win32Application Wizard"Dialog Box"Overview"Page, click"Next step".
- 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.
- In"Win32Application Wizard"Of"Application settings"Page, select"Additional options"Under"Empty Project".
- Click"Complete"Create a project.
Add a class to the dynamic link library
- 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.
- 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 );
};
}
- 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.
- 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.
- "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;
}
}
- 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 ). |
- 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
- To create an application that will reference and use the newly created dynamic link library"File"Select"New"And then select"Project...".
- In"Project Type"In the pane, select"Visual c ++"Under"Win32".
- In"Template"In the pane, select"Win32Console Application".
- 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.
- Click"OK"Start"Win32Application Wizard". In"Win32Application Wizard"Dialog Box"Overview"Page, click"Next step".
- In"Win32Application Wizard"Of"Application settings"Page, select"Application Type"Under"Console Application".
- In"Win32Application Wizard"Of"Application settings"Page, clear"Additional options"Under"Precompiled header"Check box.
- Press"Complete"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 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 ".
- 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".
- 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.
- 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. |
- 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;
}
- From"Generate"Select"Generate a solution"To generate an executable file.
Run the application
- 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".
- 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