Discover how to use separate projects within a solution to reduce confusion when you have imported code mixed with your new code.
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.
int add(int a, int b);
int minus(int a, int b);
int add(int a, int b)
c = a+b;
int minus(int a, int b)
c = a-b;
using namespace std;
int main(int argc, char* argv)
p = add(10,3);
q = minus(10,3);
cout << "Testing tool box library.\n";
cout << "p=" << p << " q=" << q << endl;
First, here are the steps needed to build the application:
- Create a new Toolbox solution with Toolbox project to generate a library from tools.h and tools.cpp.
- Add tools.h and tools.cpp into project.
- Create MyApp (Console program) project in the same solution.
- Set up MyApp directory and dependencies so it will link with the generated Toolbox.lib library.
- 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.
Click OK to go to the next page as shown below. Select Static Library and click Finish.
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.
The following items will now be in your project folders.
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.
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.
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.
In the second dialog page, select Console application and then click Finish to create the project.
The Solution and Project folders should now look like the screen below.
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.
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.
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.
Next, please enter 'Toolbox.lib' into: Configuration Properties->Linker->Input->Additional Dependencies, as shown in the screen shot below.
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.
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.
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.
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.
The application will now run in a console window, as shown in the screen shot below, confirming that everything is working.