• RSS Feed

Add #include Directive

If you hover over an undeclared identifier in C/C++, choose "Add include" from a refactoring menu (Shift+Alt+Q) to insert the appropriate directive.

The directive is inserted after the last #include in your current file, outside of any #ifdef directives. The position of the text caret is unchanged.

Except for a few system libraries such as MFC and CRT, Visual Assist can only suggest the including of headers it has parsed. If "Add include" is missing from—or disabled in—a refactoring menu, Visual Assist has not parsed the header. 

Add appropriate directory(ies) to your project settings and manually include the directory(ies) somewhere in your project or solution. Once included in one location, Visual Assist can suggest including the headers in other locations.

Umbrella Headers in C/C++

In most cases, Visual Assist adds the header that contains the definition of a symbol, not an umbrella header that includes it and other related headers. There are a few exceptions for several popular libraries, including Boost and Qt. In these cases, Visual Assist suggests adding the umbrella header.

Note: Except for a few system libraries such as MFC and CRT, Visual Assist can only suggest to include headers if it has parsed them, including the headers for Boost and Qt. Headers for Boost or Qt should be listed in the VC++ Directories of your project so they are considered stable, and included somewhere in a solution. Once included in one location, Visual Assist can suggest including them in other locations.

Add Include Style in C/C++

When using the Add Include feature, Visual Assist automatically determines an appropriate path to a header and the tokens that surround it in an #include directive. Example directives include:

#include "filename1.h"
#include "directory\filename1.h"
#include "..\filename1.h"
#include <filename2.h>
#include <directory\filename2.h>

You can tell Visual Assist to use only one token, i.e., double quote or angle bracket, and to limit headers to filenames only.

If you tell Visual Assist to use only double quotes, the example directives become:

#include "filename1.h"
#include "directory\filename1.h"
#include "..\filename1.h"
#include "filename2.h"
#include "directory\filename2.h"

If you tell Visual Assist to limit headers to filenames, i.e., omits paths, the example directives become:

#include "filename1.h"
#include <filename2.h>

If you tell Visual Assist to use only double quotes and limit headers to filenames, the example directives become:

#include "filename1.h"
#include "filename2.h"

You can specify your preferred style—token and filename—in the options dialog of Visual Assist.

File Delimiter

Visual Assist inserts include directives whose path delimiter, '/' or '\', is identical to the delimiter of existing #include directives in the file. If the current file does not contain any #include directives, or none of the existing directives use a path delimiter, Visual Assist uses a delimiter based on the current IDE.

Visual Studio 2010 and newer

Visual Assist respects the setting in the options dialog of the IDE.


Visual Studio 2008 and older

Set the fallback path delimiter in the registry.

Needless #includes in C/C++

Add Include makes the addition of headers convenient but you should rely on compiler errors and your knowledge of header hierarchy to decide if a #include is required.

Do not to add headers needlessly. Visual Assist lets you add the header that contains the definition of a symbol even if you have already included an umbrella header.

Registry Settings

Value Name Meaning
AddIncludePreferShortestRelativePath Make Add Include refrain from giving precedence to shortest path
AddIncludeTokenStyle Make Add Include use a specific token or include by filename
DefaultAddIncludeDelimiter Set fallback path delimiter for Add Include