Introduction
One of the general goals of most programmers is to re-use code. In order to re-use code, our current project must be able to include it in the build process This programming tip presents a difficulty I found with this option.
Background
When writing my Server and Client TCP/IP applications, the Server and Client code I wrote needed some test projects for development. The core code was several classes that are to be included in other projects. I did not want to put a fresh copy in every project that used them, so they needed to be in a special directory for re-used code. The method of getting access to them in the test project is via the properties. There is a problem with that, hence, this programming tip.
Here is a link to the article and project that prompted this need: My TCP/IP article.
Using the Tip
There are two methods to walk through this tip. First, start your own project. Create a directory somewhere other than within the solution directory to store code that you want to include in multiple solutions / projects.
Second, download the solution from my article I just referenced. Due to the manner that VS (Visual Studio) uses to reference the additional include, you will have to follow these steps to get a good build.
At this point, I presume that you have completed one of the two above operations and have:
- a solution ready to build and
- a separate directory with code or classes ready to incorporate in the projects of your solution
The operations will be the same for both, so I use my referenced solution as the vehicle to demonstrate this.
Open the solution and right click on a project and select properties. In the "Explorer" section of the Property Pages dialog, select C/C++ as shown in the image below. Note: Other languages may be different.
At the top of the dialog is the field Additional Include Directories. The body of that field has my include directory D:\COMMON_CODE. Replace my entry with the path to your common code directory, then click OK.
If you are using my project, expand the project named Project_Client_
to get the image below. If you are using your own solution and project, you should be able to find the similarities.
In section Header Files, there are six files that are in the additional include directory. They are:
- C_Log_Writer.h
- C_TCP_Client.h
- C_TCP_Constants.h
- C_TCP_Format_WSA_Text.h
- C_TCP_IADS_Typedefs.h
- Gnu_Notice.h
There are not as many in the source files, but you can figure them out.
If a build is attempted now, it will fail because the compiler cannot find these include files and their associated dot CPP files.
Why Not! I cried with frustration! They are exactly where the properties dialog says to find them.
That answer can be found within the file Project_Client.vcproj. (As needed, change the name of your file to match the project but the extension will be the same.) Edit that file and search for the text: Additional to find something like this:
AdditionalIncludeDirectories="D:\COMMON_CODE"
All well and good. Now search for one of the include files and find a line that looks something like this:
RelativePath="..\..\..\..\COMMON_CODE\C_Log_Writer.h"
There is an entry for each of the files from the include directory. This is the problem. When we type in the absolute path, VS remembers that. When we add a file in using the Add -> Existing Item stuff, VS uses the relative path. After VS implores us to enter the absolute path, it generated a relative path and ignored our entry. That was a bit unfriendly.
To resolve this, close the dot vsproj file and return to the solution explorer. Remove each of the files from the solution, then add them back in. That will update the dot vsproj file. Don't forget that there are two projects in this solution.
That is it. Happy coding.
History
- 4 May 2013: Original post