How to include sub-directories in Visual Studio.

ajay jadhav 1 Reputation point
2022-01-21T14:51:19.877+00:00

I have to include many header files, which are in different sub-directories. Is there a way in Visual Studio (I am using 2013 edition) to set one include path that Visual Studio will search also the sub-directories for header files?
My Project is in C/C++.
Add the "base folder" to the project (project properties -> Configuration Properties -> C/C++ -> Additional Include Directories, "additional include directories")

I have tried above option but it is not possible for me to add each and every directory followed by a semicolon.
I have total 60 + different C C++ sub-directories

Developer technologies C++
Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-01-21T16:13:29.697+00:00

    Unfortunately that is the way C++ works. Every include path must be explicitly listed. This is the nature of the beast. What most people do is include the base path and then use relative paths in the source files from there. For example assume the following:

    \somelib
         \audio
         \graphics
         \network
    

    Add the somelib path as an include path to the compiler.

    #include "audio\audio.h"
    #include "graphics\graphics.h"
    #include "network\network.h"
    

    Given that you're on VS 2013 still that is about the extent of your options. However if you upgrade to newer VS versions then you can start taking advantage of newer C++ features that will help move you away from this mess. One of the features I like is using vcpkg which handles at least the include/linking stuff for you in many cases. In the latest version of VS/C++ then you can start looking into modules which is a step away from includes. But all this requires more significant change to your code and won't help with any of your custom stuff.

    Another option is to create a wrapper header file that includes the relative path files you care about (as shown above). Then reference that include file first in your source files instead. This would consolidate the pathing but requires you to update your files.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.