Creating and Using a Static Library

简介:
 
 
Walkthrough: Creating and Using a Static Library 

In this walkthrough, you will create a static library (LIB) containing useful routines that can be used by other applications. Using static libraries is a great way to reuse code. Rather than re-implementing these routines in every program you create, you write them once and reference them from applications that need the functionality.

This walkthrough uses native C++. For a walkthrough using native C++ to create a dynamic link library (DLL), see Walkthrough: Creating and Using a Dynamic Link Library. For a walkthrough using Visual C++ that targets the Common Language Runtime, seeWalkthrough: Creating and Using a Managed Assembly.

This walkthrough covers the following:

  • Creating a new static library project

  • Adding a class to the static library

  • Creating an application that references the static library

  • Using the functionality from the static library in the console application

  • Running the application

This topic assumes you understand the fundamentals of the C++ language.

To create a new static library project

  1. From the File menu, select New and then Project….

  2. From the Project types pane, under Visual C++, select Win32.

  3. From the Templates pane, select Win32 Console Application.

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

  5. Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application Wizard dialog, pressNext.

  6. From the Application Settings page of the Win32 Application Wizard, under Application type, select Static library.

  7. From the Application Settings page of the Win32 Application Wizard, under Additional options, deselectPrecompiled header.

  8. Press Finish to create the project.

To add a class to the static library

  1. To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsLib.h, and press Add. A blank file will be displayed.

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

    // 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);
        };
    }
  3. To create a source file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as MathFuncsLib.cpp, and press Add. A blank file will be displayed.

  4. Implement the functionality for MyMathFuncs in the source file. The code should resemble the following:

    // 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;
        }
    }
  5. To build the project into a static library, from the Project menu, select MathFuncsLib Properties…. From the left pane, under Configuration Properties, select General. From the right pane, change the Configuration Type to Static Library (.lib). Press OK to save the changes.

    NoteNote

    If building from the command line, you must build the program in two steps. First, compile the code using Cl.exe with the /c compiler option (cl /c /EHsc MathFuncsLib.cpp). This will create an object file named MathFuncsLib.obj. For more information, see /c (Compile Without Linking). Second, link the code using the Library Manager Lib.exe (lib MathFuncsLib.obj). This will create the static library MathFuncsLib.lib. For more information on the Library Manager, see LIB Reference.

  6. 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 an application that references the static library

  1. To create an application that will reference and use the static library that was just created, from the File menu, selectNew and then Project….

  2. From the Project types pane, under Visual C++, select Win32.

  3. From the Templates pane, select Win32 Console Application.

  4. Choose a name for the project, such as MyExecRefsLib, and enter 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 static library.

  5. Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.

  6. From the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.

  7. From the Application Settings page of the Win32 Application Wizard, under Additional options, deselectPrecompiled header.

  8. Press Finish to create the project.

To use the functionality from the static library in the console application

  1. After you create a new Console Application, an empty program is created for you. The name for the source file will be the same as the name you chose for the project above. In this example, it is named MyExecRefsLib.cpp.

  2. To use the math routines that were created in the static library, you must reference it. To do this, select References…from the Project menu. From the Property Pages dialog, expand the Common Properties node and select References. Then select the Add New Reference… button. For more information on the References… dialog, see References, Common Properties, <Projectname> Property Pages Dialog Box.

  3. The Add Reference dialog 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. From the Projects tab, selectMathFuncsLib. Then select OK. For more information on the Add Reference dialog, see Add Reference Dialog Box.

  4. To reference the header files of the static library, you must modify the include directories path. To do this, from theProperty Pages dialog, expand the Configuration Properties node, then the C/C++ node, and select General. Next toAdditional Include Directories, type in the path to the location of the MathFuncsLib.h header file.

  5. You can now use the MyMathFuncs class in this application. 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;
    }
  6. Build the executable by selecting Build Solution from the Build menu.

To run the application

  1. Make sure MyExecRefsLib is selected as the default project. From the Solution Explorer, select MyExecRefsLib, and then select Set As StartUp Project from the Project menu.

  2. To run the project, select Start Without Debugging from the Debug menu. The output should look like this:

    a + b = 106.4
    a - b = -91.6
    a * b = 732.6
    a / b = 0.0747475
本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2010/03/04/1678466.html ,如需转载请自行联系原作者
相关文章
|
12天前
|
人工智能 机器人 测试技术
【CMake报错】Cannot specify compile definitions for target “PRIVATE“ which is not built...
【CMake报错】Cannot specify compile definitions for target “PRIVATE“ which is not built...
|
5月前
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
|
9月前
error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
195 0
|
10月前
|
关系型数据库 MySQL Linux
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or
static library libs/libvpx/libvpx.a is not portable!
static library libs/libvpx/libvpx.a is not portable!
285 0
dyld Library not loaded Reason image not found 问题解决
添加第三方框架,然后启动app的时候会,提示dyld: Library not loaded: Reason: image not found 网上大部分的做法都是把Build Phases 里对应framework后边的选项修改成为Optional,但这个是治标不治本,还是没法解决问题
151 0
error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
87 0
error while loading shared libraries: libxx.so: cannot open shared object file: No such file
error while loading shared libraries: libxx.so: cannot open shared object file: No such file
80 0
|
C++
configure: error: libmpfr not found or uses a different ABI (including static vs shared).
配置mpc的时候提示此错误: configure: error: libmpfr not found or uses a different ABI (including static vs shared).
2662 0