How to Create and Implement Static Library in VC++

Objective

To create a simple static library and implement it in a Console Application.

Requeriments

Visual Studio C++ 2008 Express Edition

Steps

  1. Create new project:

    1.png
     
  2.  Select the Project type as Win32. Template as Win32 Console Application. I set the name of the project as MyMathLib. After you set the name press OK.
    2.jpg

     
  3. The Win32 Application Wizard dialog will pop-up. Press Next, and check on the Application type option Static Library, and in "Additional options" you can uncheck precompiled options.

    3.png
     
  4. From here you will have to write the code for your classes, properties and functions that you need to implement. Let's write some simple functions for our libraries. First we need to create one header file to declare the two functions. These functions will be named add and multiply.

    Each function will receive two integer arguments, and return an integer value.
     

    #ifndef __MY_MATH_LIB__

    #define __MY_MATH_LIB__

     

    int add(int number_a, int number_b); 

    int multiply(int number_a,int number_b); 

    #endif

    Now that they are declared, let's write the definition of our functions in a .cpp file:
     

    #include"MyMathLib.h"

     

    int add(int number_a, int number_b)

    {

      return (number_a) + (number_b);

    }

     

    int multiply(int number_a,int number_b)

    {

      return (number_a) * (number_b);

    }

    Press F7 to build the library solution. If we go to our Debug or Release folder (depending our compiling configuration), our output file will be the static library. That means our library can be implemented in any Visual C++ project. Let's use these functions in a Console Application.
     

  5. Go to Solution Explorer, and right-click on the solution item, then Add -> New Project.

    4.png
     
  6. Set the project type as Win32 and the template Win32 Console Application. Then click OK. I named my project TestMyMathLib.

    5.png
     
  7. In the Win32 Application Wizard window, press Next. Make sure the Application type is Console Application and in the Additional Options that empty is selected.

    6.png
     
  8. In the Solution Explorer go to your Win32 Application project root item, right-click, and in the submenu click on the option "Set as StartUp Project" .

    7.png
     
  9. Before starting to write any code in the Win32 application project, we need to set our Static Library Project as a dependency. So again, in the Solution Explorer go to your Win32 Application project root item, press right-click, and in the submenu click on the option Project Dependencies.

    8.png
     
  10. The Project Dependencies window will pop-up. On the Dependencies tab, make sure that your Win32 App Project is selected. Then on the list of dependencies select your Static Library Project.

    9.png
     
  11. On the Build Order tab, make sure your Static Library Project is built before your Win32 App Project.

    10.png
     
  12. Create a cpp file for Win32 Application project and write the following code:

    #include<iostream>

    #include"../MyMathLib/MyMathLib.h" //Path of your declared methods

    usingnamespace std;

     

    int main ()

    {

       cout <<"Testing my math lib" << endl; 

       cout <<"Please enter a number "

      int number_a, number_b; 

       cin >> number_a; 

       cout <<"Please enter another number "

       cin >> number_b; 

       cout <<"The addition result for these numbers is "

       cout << add(number_a, number_b); 

       cout << endl; 

       cout <<"The multiply result for these numbers is "

       cout << multiply(number_a, number_b); 

       cout << endl; 

       system("pause"); 

      return 0; 

    }
     

  13. Press F7 to build the solution. And press F5 to run and debug your application.
    This is how the application should look.

    11.png


Similar Articles