Walkthrough: Creating and Using a Dynamic Link Library (C++)
The first type of library we will create is a dynamic link library (DLL). Using DLLs is a great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them one time and reference them from applications that need the functionality.
This walkthrough covers the following:
Creating a new dynamic link library (DLL) project.
Adding a class to the dynamic link library.
Creating an application that references the dynamic link library.
Using the functionality from the class library in the console application.
Running the application.
To create a new dynamic link library (DLL) project
From the File menu, select New and then Project….
On the Project types pane, under Visual C++, select Win32.
On the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MathFuncsDll, and type it in the Name field. Choose a name for the solution, such as DynamicLibrary, and type it in 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 of the Win32 Application Wizard, under Application type, select DLL if it is available or Console application if DLL is not available. Some versions of Visual Studio do not support creating a DLL project by using wizards. You can change this later to make your project compile into a DLL.
On the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty project.
Click Finish to create the project.
To add a class to the dynamic link library
To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. On the Categories pane, under Visual C++, select Code. On the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsDll.h, and click Add. A blank file will be displayed.
Add a simple class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. The code should resemble the following:
// 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 the method declarations in this code. These modifiers enable the method to be exported by the DLL so that it can be used by other applications. For more information, see dllexport, dllimport.
To create a source file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. On the Categories pane, under Visual C++, select Code. On the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as MathFuncsDll.cpp, and click Add. A blank file will be displayed.
Implement the functionality for 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 - 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 build the project into a DLL, from the Project menu, select MathFuncsDll Properties…. On the left pane, under Configuration Properties, select General. On the right pane, change the Configuration Type to Dynamic Library (.dll). Click OK to save the changes.
NoteIf you are building 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 Run-Time Library).
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 the dynamic link library
To create an application that will reference and use the dynamic link library that you just created, from the File menu, select New and then Project….
On the Project types pane, under Visual C++, select Win32.
On the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MyExecRefsDll, and type it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the dynamic link library.
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, under Application type, select Console application.
On the Application Settings page of the Win32 Application Wizard, under Additional options, clear the Precompiled header check box.
Press Finish to create the project.
To use the functionality from the class library in the console application
After you create a new console application, an empty program is created for you. The name for the source file is the same as the name that you chose for the project earlier. In this example, it is named MyExecRefsDll.cpp.
To use the math routines that were created in the dynamic link library, you must reference the library. To do this, select the MyExecRefsDll project in the Solution Explorer, then select References… from the Project menu. On the Property Pages dialog box, expand the Common Properties node, select Framework and References, and then select the Add New Reference… button. For more information about the References… dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.
The Add Reference dialog box is displayed. This dialog lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. On the Projects tab, select MathFuncsDll. Then click OK.
To reference the header files of the dynamic link library, you must modify the include directories path. To do this, on the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include Directories, type the path of the location of the MathFuncsDll.h header file.
The executable does not load dynamic link libraries until runtime. You must tell the system where to locate MathFuncsDll.dll. You do so by using the PATH environment variable. To do this, on the Property Pages dialog box, expand the Configuration Properties node and select Debugging. Next to Environment, type the following: PATH=<path of MathFuncsDll.dll file>, where <path of MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll. Click OK to save all the changes.
NoteIf you want to run the executable from the command line instead of from Visual Studio, you must manually update the PATH environment variable from the command prompt as follows: set PATH=%PATH%;<path of MathFuncsDll.dll file>, where <path of MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll.
You can now use the MyMathFuncs class in this 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 = 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; }Build the executable by selecting Build Solution from the Build menu.
To run the application
Make sure MyExecRefsDll is selected as the default project. In the Solution Explorer, select MyExecRefsDll, and then select Set As StartUp Project from the Project menu.
To run the project, select Start Without Debugging from the Debug menu. The output should resemble this:
a + b = 106.4 a - b = -91.6 a * b = 732.6 a / b = 0.0747475
- 5/13/2012
- stn
- 12/26/2011
- worksofcraft
- 12/26/2011
- worksofcraft
Suppose if i want to do the same thing like making different solutions then how do i remove this error? what are the steps???
I cut and pasted all the code provided in the example, and added nothing else. I used the same filenames. But I ended up with linker error LNK2019. The IDE is Visual Studio 2010, academic version. Here's the output:
1>MyExecRefsDll.obj : error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Divide(double,double)" (?Divide@MyMathFuncs@MathFuncs@@SANNN@Z) referenced in function _main
1>MyExecRefsDll.obj : error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Multiply(double,double)" (?Multiply@MyMathFuncs@MathFuncs@@SANNN@Z) referenced in function _main
1>MyExecRefsDll.obj : error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Subtract(double,double)" (?Subtract@MyMathFuncs@MathFuncs@@SANNN@Z) referenced in function _main
1>MyExecRefsDll.obj : error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Add(double,double)" (?Add@MyMathFuncs@MathFuncs@@SANNN@Z) referenced in function _main
1>C:\Visual Studio Applications\MyExecRefsDll.cpp\Debug\MyExecRefsDll.cpp.exe : fatal error LNK1120: 4 unresolved externals
- 1/29/2011
- dominant_7th
- 10/30/2011
- Waqar Ali Khan
- 10/25/2011
- barrett bonden
I have the same problem...
Maybe it's explained here? http://stackoverflow.com/questions/1297013/why-do-we-still-need-a-lib-stub-file-when-weve-got-the-actual-dll-implementati
If that's the case, why MS didn't explain that??!
After trying creating a non-empty project I've also found this inside the readme.txt file:
[...]
myDLLproject.cpp
This is the main DLL source file.
When created, this DLL does not export any symbols. As a result, it
will not produce a .lib file when it is built. If you wish this project
to be a project dependency of some other project, you will either need to
add code to export some symbols from the DLL so that an export library
will be produced, or you can set the Ignore Input Library property to Yes
on the General propert page of the Linker folder in the project's Property
Pages dialog box.
[...]
- 8/27/2011
- Hopworks
- 8/12/2011
- Amare1982
- 6/26/2011
- Dan M 6
#include "MathFuncsDll.h"
provide complete path of your "mathFuncsDll.h" like #include "D:\MathFuncsDll\MathFuncsDllNew.h"and then
Rebuild MathFuncsDll and MyExecRefsDll and Run.
Guideline is wrong: replace the step 3 instruction "select Win32 Console Application" with "select Win32 Project"
(look at http://social.msdn.microsoft.com/Forums/en-CA/Vsexpressvc/thread/8614e913-06a1-4646-a734-e4f596618c3d)
- 5/3/2011
- Dr Math
- 4/23/2011
- hugoestr
- 4/20/2011
- Sudheersamudrala
I too am having trouble getting my VC++ 2010 .dll to work in
another class project. Just so happens
mine is a VB2010 class project. Can
anyone provide a link to how to I can do this?
I cannot make a reference to the VC++ project or .dll, even though the
project is within my solution of VB projects.
- 3/7/2011
- Christopher M. lauer
Is there a walkthrough example for accessing a C++ Win32 (MathFun) DLL from a C# client app (preferably a WinForm .Net 4.0)?
- 1/13/2011
- VVXBos