Export (0) Print
Expand All
16 out of 31 rated this helpful - Rate this topic

Walkthrough: Creating and Using a Static Library (C++)

This step-by-step walkthrough shows how to create a static library (a .lib file) for use with C++ apps. Using a static library is a great way to reuse code. Rather than re-implementing the same routines in every app that requires the functionality, you write them one time in a static library and then reference it from the apps. Code linked from a static library becomes part of your app—you don’t have to install another file to use the code.

This walkthrough covers these tasks:

  • Creating a static library project.

  • Adding a class to the static library.

  • Creating a console app that references the static library.

  • Using the functionality from the static library in the app.

  • Running the app.

An understanding of the fundamentals of the C++ language.

NoteNote

If you are using an Express edition that does not display a Build menu, on the menu bar, choose Tools, Settings, Expert Settings to enable it.

To create a static library project

  1. On the menu bar, choose File, New, Project.

  2. In the left pane of the New Project dialog box, expand Installed Templates, Visual C++, and then select Win32.

  3. In the center pane, select Win32 Console Application.

  4. Specify a name for the project—for example, MathFuncsLib—in the Name box. Specify a name for the solution—for example, StaticLibrary—in the Solution Name box. Choose the OK button.

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  6. On the Application Settings page, under Application type, select Static library.

  7. On the Application Settings page, under Additional options, clear the Precompiled header check box.

  8. Choose the Finish button to create the project.

To add a class to the static library

  1. To create a header file for a new class, open the shortcut menu for the MathFuncsLib project in Solution Explorer, and then choose Add, New Item. In the Add New Item dialog box, in the left pane, under Visual C++, select Code. In the center pane, select Header File (.h). Specify a name for the header file—for example, MathFuncsLib.h—and then choose the Add button. A blank header file is displayed.

  2. Add a class named MyMathFuncs to do common mathematical operations such as addition, subtraction, multiplication, and division. The code should resemble this:

    // 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
            static double Divide(double a, double b);
        };
    }
    
    
  3. To create a source file for the new class, open the shortcut menu for the MathFuncsLib project in Solution Explorer, and then choose Add, New Item. In the Add New Item dialog box, in the left pane, under Visual C++, select Code. In the center pane, select C++ File (.cpp). Specify a name for the source file—for example, MathFuncsLib.cpp—and then choose the Add button. A blank source file is displayed.

  4. Use this source file to implement the functionality for MyMathFuncs. The code should resemble this:

    // 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)
        {
            return a / b;
        }
    }
    
  5. Compile the static library by selecting Build, Build Solution on the menu bar. This creates a static library that can be used by other programs.

    NoteNote

    When you build on the Visual Studio command line, you must build the program in two steps. First, run cl /c /EHsc MathFuncsLib.cpp to compile the code and create an object file that's named MathFuncsLib.obj. (The cl command invokes the compiler, Cl.exe, and the /c option specifies compile without linking. For more information, see /c (Compile Without Linking).) Second, run lib MathFuncsLib.obj to link the code and create the static library MathFuncsLib.lib. (The lib command invokes the Library Manager, Lib.exe. For more information, see LIB Reference.)

To create a C++ console app that references the static library

  1. Open the shortcut menu for the StaticLibrary solution in Solution Explorer, and then choose Add, New Project.

  2. In the left pane, under Visual C++, select Win32.

  3. In the center pane, select Win32 Console Application.

  4. Specify a name for the project—for example, MyExecRefsLib—in the Name box. Choose the OK button.

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  6. On the Application Settings page, under Application type, select Console application.

  7. Under Additional options, clear the Precompiled header check box.

  8. Choose the Finish button to create the project.

To use the functionality from the static library in the app

  1. After you create a console app, an empty program is created for you. The name for the source file is the same as the name that you chose earlier. In this example, it's named MyExecRefsLib.cpp.

  2. Before you can use the math routines in the static library, you must reference it. To do this, open the shortcut menu for the MyExecRefsLib project in Solution Explorer, and then choose References. In the MyExecRefsLib Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.

  3. The Add Reference dialog box lists the libraries that you can reference. The Projects tab lists the projects in the current solution and any libraries that they contain. On the Projects tab, select the MathFuncsLib project, and then choose the OK button.

  4. To reference the MathFuncsLib.h header file, you must modify the included directories path. In the Property Pages dialog box for MyExecRefsLib, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include Directories, specify the path of the MathFuncsLib directory or browse for it.

    To browse for the directory path, choose the down arrow button to open the Additional Include Directories property value drop-down list box, and then choose <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, navigate to the solution directory, select the MathFuncsLib directory and then choose the Select Folder button to save your selection. In the Additional Include Directories dialog box, choose the OK button, and then in the Property Pages dialog box, choose the OK button to save your changes to the project.

  5. You can now use the MyMathFuncs class in this app. To do this, replace the contents of MyExecRefsLib.cpp with this 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;
    }
    
  6. Build the executable by choosing Build, Build Solution on the menu bar.

To run the application

  1. Make sure that MyExecRefsLib is selected as the default project by opening the shortcut menu for MyExecRefsLib in Solution Explorer, and then choosing Set as StartUp Project.

  2. To run the project, on the menu bar, choose Debug, Start Without Debugging. The output should resemble this:

    a + b = 106.4
    a - b = -91.6
    a * b = 732.6
    a / b = 0.0747475
    
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.