TIP: Utilizing Solutions and Projects in VS 2005

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

As programmers we try to improve productivity—re-using existing codes or libraries is a must. In its simplest form, a library could arrive in the form of two files, such as widget.h and widget.lib, that can be included and linked into your new application. Alternatively, they might arrive as a source code that can be added to your application. This sounds simple enough except that this code might be updated regularly and you will want to keep it separate in a folder—as a standalone project in Visual Studio.

This leads to the topic of this article. How you can utilize projects in a VS2005 solution to keep everything nicely organized?

For this tutorial, let’s just say you have received the two files (tools.h and tools.cpp) as listed below and you want to be able to call add() and minus() in your application. Obviously, real libraries would probably have hundreds of functions rather than just the couple shown here. Last but not least, your application (MyApp.cpp) would call the two functions.

tools.h:
———————————————————–

int add(int a, int b);
int minus(int a, int b);

tools.cpp:
———————————————————–

#include "tools.h"

int add(int a, int b)
{
   int c;
   c = a+b;
   return c;
}

int minus(int a, int b)
{
   int c;
   c = a-b;
   return c;
}

MyApp.cpp:
———————————————————–

#include <iostream>
#include "..toolstools.h"
using namespace std;

int main(int argc, char* argv[])
{
   int p,q;
   p = add(10,3);
   q = minus(10,3);
   cout << "Testing tool box library.n";
   cout << "p=" << p << " q=" << q << endl;
   return 0;
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read