Add Include

We recently released a feature for C/C++ that has long been requested: Add Include.  A lot of work had previously been put into our #include completion listboxes – but that work is moot because now you don’t need to type #includes directives by hand.

The Add Include command is available when you place the caret on a symbol which is declared in a header file that is not directly included in the active file.

If the header that the symbol is defined in is already directly included in the current file, the command is disabled.  Otherwise, it is enabled – even though the dependency may already be satisfied by another directive that itself #includes the header.  This is by design as many programming style guidelines mandate explicit inclusion of declarations (as opposed to happenstance or indirect inclusion).

For types and functions, Add Include straightforwardly adds an include directive for the header in which the type or function is declared.  For variables that are declared in the active file, Add Include acts on the type of the variable (no need to go to the declaration of the variable to add the include for its type).  For variables that are not declared in the active file, Add Include operates in the same way as it does for types and functions.

While the command is available in the usual VA Refactor menus, the easiest way to invoke it (at least for keyboardists) is to place the caret on a symbol and press alt+x, r, n.

12 thoughts on “Add Include

  1. This product just gets more an more awesome!

    I can not imagine writing code without your plugin ever again!

    Now if we could eliminate #includes that we don’t actually reference!

  2. add include is great but… there’s always a but! It would be really nice if it copied the case of the filename.
    My nicely named “CamelCapsFileNames.h” become rather flat “camelcapsfilenames.h” and they’re a little tricky to ready at times.

    I’ve written a piece of VBScript for Visual Studio 2003-2008 which does do some of the work of removing unused includes. I’ve used it a lot but it can be a little long winded. It basically runs through a file (or even every file in a solution!), knocking out the #includes one by one and checking the compile. If you have biiig files then it’s a pain, if you have nice reasonable size classes then it works fine.

    If you want a copy then let me know.

  3. I love your program. Its the best addin made for an IDE. Although recently I ditched windows and Visual studio is no more useful to me.

    I am now using Xcode for mac. It would be a dream come true if you guys could bring the same functionality in Xcode.

  4. This is great! I use it all the time.

    One annoyance (beside the file name case issue mentioned above) is that when I do this on std::string, it adds:


    Then I have to edit it to be just for standards conformance.

  5. One more thing: when it adds an include using a relative path, it uses backslashes for the path separator, leading to non-portable code. For example, instead of this:

    #include “..\MyClass.h”

    I’d like to see

    #include “../MyClass.h”

  6. @negritot: Before using the default path delimiter, VA will look for a delimiter in other #include statements in the current file. If none is found, then VA uses the default. Starting in build 1734, you can change the default via modification of the DefaultAddIncludeDelimiter registry value.

  7. Idea:

    Suggest a minimal set of include files that is needed to compile the current file. This would be useful to clean-up existing source code.

    The existing #include lines can be used to find out the places where all of the symbols in the file are declared, and based on the findings a new minimal set of include files can be synthesized that would give access to those same symbols. The new set of include files then can replace the existing #include directives. This could e.g. neatly clean-up include files which are included only because they happen to include another file that has the required declarations.

    (This probably sounds easier than it is, but it seems that Add Include already solves a lot of the problems that would come up in such feature.)

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s