78 2. Tools of the Trade
Copy an Existing Project
If I am creating a project that is similar to an existing project that I know al-
ready works, I’ll oft en just copy that .vcproj fi le and then modify it as neces-
sary. In Visual Studio 2005, this is very easy. You simply copy the .vcproj fi le
on disk, then add the newly copied project to your solution by right-clicking
the solution in the Solution Explorer and selecting “Add...” and “Existing
project...” from the pop-up menus.
One caveat when copying project fi les is that the name of the project is
stored inside the .vcproj fi le itself. So when you load up the new project for the
fi rst time in Visual Studio 2005, it will still have the original name. To rectify
this, you can simply select the project in the Solution Explorer window, and
hit F2 to rename it appropriately.
Another problem arises when the name of the executable, library, or DLL
that the project creates is specifi ed explicitly in the .vcproj fi le. For example,
the executable might be specifi ed as “C:\MyGame\bin\MyGame.exe” or
“$(OutDir)\MyGame.exe.” In this case, you’ll need to open the .vcproj fi le
and do a global search-and-replace of the executable, library, or DLL name
and/or its directory path. This is not too diffi cult. Project fi les are XML, so you
can rename your copied .vcproj fi le to have an “.xml” extension and then open
it in Visual Studio (or any other XML or raw text editor). One elegant solution
to this problem is to use Visual Studio’s macro system when specifying all out-
put fi les in your project. For example, if you specify the output executable as
“$(OutDir)\$(ProjectName).exe”, then the project’s name will automati-
cally be refl ected in the name of the output executable fi le.
I should mention that using a text editor to manipulate .vcproj fi les is not
always to be avoided. In fact, the practice is quite common, at least in my ex-
perience. For example, let’s say you decided to move the folder containing all
of your graphics header fi les to a new path on disk. Rather than manually open
each project in turn, open the Project Property Pages window, navigate to the
C/C++ tab, and fi nally update the include path manually, it’s much easier and
less error-prone to edit the fi les as XML text and do a search-and-replace. You
can even do a “Replace in fi les” operation in Visual Studio for mass edits.
2.2.5. Debugging Your Code
One of the most important skills any programmer can learn is how to eff ec-
tively debug code. This section provides some useful debugging tips and
tricks. Some are applicable to any debugger and some are specifi c to Microsoft
Visual Studio. However, you can usually fi nd an equivalent to Visual Studio’s
debugging features in other debuggers, so this section should prove useful
even if you don’t use Visual Studio to debug your code.