TIP: Utilizing Solutions and Projects in VS 2005

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 "..\tools\tools.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;
}

TIP: Utilizing Solutions and Projects in VS 2005

Steps Required

First, here are the steps needed to build the application:

  1. Create a new Toolbox solution with Toolbox project to generate a library from tools.h and tools.cpp.
  2. Add tools.h and tools.cpp into project.
  3. Create MyApp (Console program) project in the same solution.
  4. Set up MyApp directory and dependencies so it will link with the generated Toolbox.lib library.
  5. Build and test MyApp.exe.

Building The Library

Start off by creating a solution using the Visual Studio 2005 new project wizard. You start the wizard by selecting File->New->Project... from the menus. A New Project dialogs as shown below will pop up. Type "Toolbox" in the entry box of Name: and the Solution Name: will be automatically filled. In this example, I am creating my Solution/Project in C:\ but you can choose any writable directory. Make sure you select Win32 as Project types and Win32 Console Application for Templates.

[01_NewProject.JPG]

Click OK to go to the next page as shown below. Select Static Library and click Finish.

[02_NewProjectFinish.JPG]

With the Toolbox project created, you can now add tools.h and tools.cpp into this project. First, using File Explorer, create a directory and place the two files in C:\Toolbox\Tools. Secondly, you need to add code to your Toolbox project as existing items, as shown in the screen shot below. Activate the menu by Right-Clicking on Source Files. When you are done, repeat this step for adding the header files.

[03_SourceAddExistingItem.JPG]

The following items will now be in your project folders.

[04_ToolboxSourceHeader.JPG]

Built Toolbox by selecting Build->Built Toolbox. The code should compile into a library called Toolbox.lib stored in C:\Toolbox\debug. Depending on your VS2005 setup, it can store this file in a different default directory but it should be easy to find it using File Explorer.

[05_ToolboxSourceHeader_Build.JPG]

TIP: Utilizing Solutions and Projects in VS 2005

Creating MyApp Application

Now that you have successfully created Toolbox.lib, it is time to generate an application that will make use of it. Right-Click on Solution 'Toolbox' to bring up and select submenus Add->New Project..., as shown in the screen shot below.

[06_SolutionAddNewProject.JPG]

Using the project wizard, create a new project called 'MyApp'. As you did earlier, select Win32 and Win32 Console Application in Project types and Templates, respectively. Press OK when your dialog looks like the one below.

[07_AddNewProject_MyApp.JPG]

In the second dialog page, select Console application and then click Finish to create the project.

[08_AddNewProject_MyApp_Finish.JPG]

The Solution and Project folders should now look like the screen below.

[09_Solution_MyApp_Toolbox.JPG]

If you now open MyApp.cpp, you will realize that it does not do anything yet. For this exercise, please download MyApp.cpp or cut and paste File3 (from Page1) of this article, and paste the code into your project's MyApp.cpp file.

The application (MyApp.cpp) is now complete but it is not quite ready for you to build as you have not defined where Toolbox.lib is located. You can confirm this by Right-Clicking on MyApp project, and selecting Build from the pop up menu. The program will compile but will have link errors, as shown in the screen shot below.

[10_Output_LNK2019Error.JPG]

TIP: Utilizing Solutions and Projects in VS 2005

Building MyApp

You will need to set up the Properties of the MyApp project — letting it know where Toolbox.lib is located. The parameter settings are in the Project Properties. Right-Click on MyApp->Properties, as shown below. However, there are two ways to enter the information. I'll go through both methods, but you will only need to choose one.

[11_MyAppProperties.JPG]

Dependencies Information: Method 1

This method allows you to manually enter the directory and library name needed to build MyApp. The Property Pages will pop up as a dialog box, as shown below. You need to enter "C:\Toolbox\debug" into the entry box of: Configuration Properties->Linker->General->Additional Library Directories.

[12_MyApp_PropertiesAddLibDir.JPG]

Next, please enter 'Toolbox.lib' into: Configuration Properties->Linker->Input->Additional Dependencies, as shown in the screen shot below.

[13_MyApp_PropertiesInputAdditionalDep.JPG]

Dependencies Information: Method 2

The library required by MyApp can also be set up via the same Properties dialog, as shown in the screen shot below. By putting a check mark in Common Properties->Project Dependencies->Toolbox, you are telling Visual Studio that Toolbox project has the required library. Please click Apply and OK to complete the setup.

[14_SolutionProjectDependenciesDlg.JPG]

You are now ready to build MyApp by selecting: Build->Build Solution from the top menu. You'll see that Tool.cpp is compiled before MyApp.cpp to build MyApp.exe. The screen show below shows the output window of VS2005.

[15_Output_BuildMyApp_Success.JPG]

TIP: Utilizing Solutions and Projects in VS 2005

The executable application is now ready for testing. Locations of all the project files are shown in the screen shot below. Two folders named MyApp and Toolbox contain the two project files. The imported files (Tools.h and Tools.cpp) are stored in a separate folder named Tools. This allows these supposedly, third party library files to be separate for easy replacement in the future. Last but not least, the Toolbox.lib and MyApp.exe files are stored in the debug folder. Obviously, if you had set up the projects as release code, the Debug folders would simply be replaced by Release versions.

[16_MyAppToolbox_ProjectFolders.JPG]

Executing MyApp

To execute MyApp.exe, just select Debug->Start Debugging. When debug is selected for the first time, VS2005 may ask you for the application to debug. In this case, just enter the full path such as: C:\Toolbox\debug\MyApp.exe, as shown in the screen shot below, and press OK.

[17_ExecutableForDebugSessionDlg.JPG]

The application will now run in a console window, as shown in the screen shot below, confirming that everything is working.

[18_MyApp_ExecDosOutputScreen.JPG]



About the Author

Swee Mok

Swee Mok is a Member of Technical Staff, working in the Applied Research and Technology Center, in Motorola Inc. His experience ranges from: Robotics and Automation, Precision flip chip and SMT component placement machines, Robotics controller, embedded software development in Linux phones and Windows Mobile, Internet remote control applications, RFID applications development, Distributed DLNA/UPnP multimedia search engine, to GPS/WiFi Hybrid Location systems. He can be reached via email: swee.mok@motorola.com.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds