The next type of library we will create is a static library (LIB). Using static libraries 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.
- Create a static library project.
- Add a class to the static library.
- Create a console application that references the static library.
- Use the functionality from the static library in the application.
- Run the application.
To create a static library 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 MathFuncsLib, and enter it in the Name field. Choose a name for the solution, such as StaticLibrary, and enter 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 Static library.
- On the Application Settings page of the Win32 Application Wizard, under Additional options, clear the Precompiled header check box.
- Click Finish to create the project.
To add a class to the static 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. From the Categories pane, underVisual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsLib.h, and then click Add. A blank file will be displayed.
- Add a class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. To do this, replace the contents ofMathFuncsLib.h with the following code.
// MathFuncsLib.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static double Add(double a, double b); // Returns a - b static double Subtract(double a, double b); // Returns a * b static double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static double Divide(double a, double b); }; }
- 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. From the Categories pane, underVisual C++, select Code. From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as MathFuncsLib.cpp, and then click Add. A blank file will be displayed.
- Implement the functionality for MyMathFuncs in the source file. To do this, replace the contents of MathFuncsLib.cpp with the following code.
// MathFuncsLib.cpp // compile with: /c /EHsc // post-build command: lib MathFuncsLib.obj #include "MathFuncsLib.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 static library, from the Project menu, select Properties. On the left pane, under Configuration Properties, select General. On the right pane, change the Configuration Type to Static Library (.lib). Click OK to save the changes.Compile the static library by selecting Build Solution from the Build menu. This creates a static library that can be used by other programs.
To create a console application that references the static library
- To create an application that will reference and use the static library that was 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 MyExecRefsLib, and type it in the Name field. Next to Solution, select Add to Solution from the drop-down list box. This will add the new project to the same solution as the static 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 Precompiled header.
- Click Finish to create the project.
To use the functionality from the static library in the application
- After you create a console application, the wizard creates an empty program for you. The name for the source file will be the same as the name that you chose for the project earlier. In this example, it is named MyExecRefsLib.cpp.
- You must reference the static library you created to use its math routines. To do this, select References from the Project menu. From the MyExecRefsLib Property Pagesdialog box, expand the Common Properties node and then click Add New Reference. 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. The Projects tab lists the projects in the current solution and any libraries that you can reference. On the Projects tab, selectMathFuncsLib. Click OK.
- To reference the MathFuncsLib.h header file, you must modify the include directories path. In the MyExecRefsLib Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. In the Additional Include Directories property value, type the path of the MathFuncsLib directory or browse for it.
To browse for the directory path, in the property value drop-down list box, click Edit. In the Additional Include Directories dialog box, in the text box, select a blank line and then click the ellipsis button (…) at the end of the line. In the Select Directory dialog box, select the MathFuncsLib directory and then click Select Folder to save your selection and close the dialog box. In the Additional Include Directories dialog box, click OK. - You can now use the MyMathFuncs class in this application. To do this, replace the contents of MyExecRefsLib.cpp with the following code.
// MyExecRefsLib.cpp // compile with: /EHsc /link MathFuncsLib.lib #include <iostream> #include "MathFuncsLib.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 MyExecRefsLib is selected as the default project. In Solution Explorer, select MyExecRefsLib, 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
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......