To create a simple static library and implement it in a Console Application.
Visual Studio C++ 2008 Express Edition
- Create new project:
- 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.
- 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.
- 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.
int add(int number_a, int number_b);
int multiply(int number_a,int number_b);
Now that they are declared, let's write the definition of our functions in a .cpp file:
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.
- Go to Solution Explorer, and right-click on the solution item, then Add -> New Project.
- Set the project type as Win32 and the template Win32 Console Application. Then click OK. I named my project TestMyMathLib.
- 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.
- 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" .
- 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.
- 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.
- On the Build Order tab, make sure your Static Library Project is built before your Win32 App Project.
- Create a cpp file for Win32 Application project and write the following code:
#include"../MyMathLib/MyMathLib.h" //Path of your declared methods
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;
- Press F7 to build the solution. And press F5 to run and debug your application.
This is how the application should look.