Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include sub-directories in Visual Studio?

I have to include many header files, which are in different sub-directories. Is there a way in Visual Studio (I am using 2005 edition) to set one include path that Visual Studio will search also the sub-directories for header files?

like image 744
kklobucki Avatar asked Dec 04 '08 09:12

kklobucki


3 Answers

Setting the folder search paths in the Visual Studio settings to fix an include issue is generally not really a good idea from a design point of view. Your code will be less portable to different machines with different directory lay-outs.

My suggestion would be to settle on an organisation of your code so that the relative paths of the sub-directories (relative to your including code) are fixed:

  • Add the "base folder" to the project (project properties -> Configuration Properties -> C/C++ -> Additional Include Directories, "additional include directories")
  • Add the subdirectories to the #include statements , i.e. #include "subdirectory/somefile.h".

This has the added bonus of being able to see which folder in your Solution contains the file - that is often useful information when you're trying to find your way around or trying to figure out what a file is for.

like image 79
Joris Timmermans Avatar answered Nov 03 '22 10:11

Joris Timmermans


We haven't found a way of setting a recursive folder search in Visual Studio (though Xcode has no problem setting and using them). What we do is list all the directories in a file, and specify the file in the C/C++ Command Line additional options:

@"IncludeFolders.rsp"

We rarely find problems using recursive search, though an accidental inclusion of ``/'' did slow things down a bit once.

like image 44
David Sykes Avatar answered Nov 03 '22 09:11

David Sykes


I've found that I can shorten path lengths in most situations, including in MSVC, by a slight of hand trick using the SUBST command. I'll show and example for shortening the default inherited C++ include paths, but you would do this for your project related include paths. First create one or more batch files like this (and run them from Windows Explorer or the command line):

MDrive.bat:
subst M: /D
subst M: "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC"

WDrive.bat:
subst W: /D
subst W: "C:\Program Files (x86)\Windows Kits\8.1\Include"

... This is just and example.  You would do this for your
    project related include directories.

You can create a number of these for your longer paths. In the case above I am only shortening the standard inherited paths that MSVC uses by default. BTW, the Subst command works w/o administrative rights, but don't step on an existing physical or mapped drive letter (I'm not sure what would happen--I don't want to test it at the moment).

This step is optional. I in MSVS 2010+ I add a custom properties sheet. BTW, if you don't know about custom properties sheets you should. See Property Pages (Visual C++). VIEW-Other Windows-Property Window, right click the project in the Property manager, Add a New Project Properties Sheet. Under User Macros, add a Macro, e.g. MyProjectIncludePaths and set the value (in this example) to

M:; M:\atlmfc\include; W:\um; W:\shared; W:\winrt

--only 49 characters in this example, which is much shorter than the default of

$(VC_IncludePath); $(WindowsSKD_IncludePath)

which upon macro expansion translates to

C:\Program Files (x86)\Windows Kits\8.1\Include;C:\Program Files (x86)\Windows Kits\8.1\Include\atlmfc\include;C:\Program Files (x86)\Windows Kits\8.1\Include\um;C:\Program Files (x86)\Windows Kits\8.1\Include\shared;C:\Program Files (x86)\Windows Kits\8.1\Include\winrt

By my count the default is 270 characters--i.e. longer than 260 characters--can anyone explain this?

In the Property Pages for the project (i.e. not the custom property sheet), in my example I went to Configuration Properties-VC++ Directories and changed the Include Directories value to $(MyProjectIncludePaths), but in practice it you would go to (in your custom property sheet page if you created one) C/C++-General and add $(MyProjectIncludePaths) to the Additional Include Directories.

In addition to the inherited 270 character paths, I have been able to add a few reasonable length paths under Additional Include Directories. Maybe the Additional Include Directories has its own length limit (?).

like image 31
riderBill Avatar answered Nov 03 '22 10:11

riderBill