Objective
To
create a simple static library and implement it on a Console
application.
Requirements
Have at least Visual
Studio C++ 2008 Express Edition installed in your PC.
Build Static Library Project
- Create a new project.
- Select Project type as General. Template as Empty Project. 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.
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 as
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
Once
we declare, 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 on our compiling configuration), our output file will
be the static library. That means our that our library can be
implemented in any Visual C++ project. Let’s use these functions in
a Console Application.
Implementing the Static Library
-
Go
to solution explorer, and do right click on the Solution Item, then
go Add -> New Project.
- Set
project type as Win32 and the template as 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 Additional Options
have selected empty.
- In
the solution explorer, go to your Win32 Application project root
item, press right click, and in submenu, click on the option Set as
StartUp Project.
- Before
you start writing any code in the Win32 App. Project, we need to set a
registry 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 submenu, click on the option Project
Dependencies...
- The
Project Dependencies windows 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 <iostream>
#include "../MyMathLib/MyMathLib.h"
using namespace 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;
}
- Press
F7 to build the solution. And press F5 to run and debug your
application. This
is how the application should look: