Compartilhar via


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

 

The latest version of this topic can be found at Walkthrough: Creating and Using a Static Library (C++).

This step-by-step walkthrough shows how to create a static library (.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 C++ console app that references the static library

  • Using the functionality from the static library in the app

  • Running the app

Prerequisites

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

Creating a static library project

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.

Adding a class to the static library

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: cl /c /EHsc MathFuncsLib.cpp
    // 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.

    Note

    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.)

Creating a C++ console app that references the static library

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

  1. On the menu bar, choose File, 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. In the drop-down list next to Solution, select Add to Solution. This adds the new project to the solution that contains the static library. 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. On the Application Settings page, under Additional options, clear the Precompiled header check box.

  8. Choose the Finish button to create the project.

Using the functionality from the static library in the app

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 MyExecRefsLibProperty 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 Adding references.

  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 check box, 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, open the property value drop-down list, and then choose Edit. In the Additional Include Directories dialog box, in the text box, select a blank line and then choose the ellipsis button () at the end of the line. In the Select Directory dialog box, select the MathFuncsLib directory and then choose Select Folder button to save your selection and close the dialog box. 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: cl /EHsc MyExecRefsLib.cpp /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.

Running the app

To run the app

  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  
    

See Also

Visual C++ Guided Tour
Walkthrough: Creating and Using a Dynamic Link Library (C++)
Desktop Applications (Visual C++)