Friday, June 21, 2013

Add your own folder structure in your VSC++ Solution

When you first create a new project(of course a solution is created at the same time) in Visual Studio, a single default folder is added. But if you are building a big project containing many .h and .cpp files, it will be more organized if you could create your own folder structure and throw the .h and .cpp files into different folders accordingly. While gaining a more clean and logic folder structure, referring to .h and .cpp files in different folders will become a new issue.

Before adding your own folder structure, all the .h and .cpp files in the same project are put in one single default folder. And you can freely add any header files you want by using #include "headerfileName.h".  But now you couldn't. So one possible solution is to put the absolute path of your project under Properties->Configuration Properties->C/C++->General->Include additional directories.  The negative side of this method is that others trying to run your program need to reconfigure the absolute path since the absolute path in your machine is not necessary the same as that in your frined's. It will become a trouble if your friend doesn't want to configure such basic environment parameters. So, a more favorable solution is to use relative path which is independent of the terminal on which your program runs.

Configuring a relative path will become a piece of cake if you understand some basic rules. And the basic rules turn out to be really basic and simple enough. Putting one dot "." in Include Additional Directories meas going to the folder where the "ProjectName.vcxproj" is located. Putting two dots ".." in Include Additional Directories means going one step up from the folder where the "ProjectName.vcxproj" is located. Now you can refer to the .h and .cpp files in your own folder structures by using #include <folder/subfolder/fileName.h>.

No comments:

Post a Comment