Fast way to insert include guard in Visual Studio

10,546

Solution 1

In visual studio 2012 use the key combinations

Ctrl+K,Ctrl+S

It allows you to surround selected code with code snippets such as:

#if, #ifdef, #ifndef, if, class, do, enum, and many more

.. or specify your own: http://msdn.microsoft.com/en-us/library/ms165394.aspx

Solution 2

#pragma once?

But no, I'm not aware of anything that automatically inserts the #ifndef, etc. for you.

Solution 3

Since you're tagging C++, you should add classes by the built-in wizard. The wizard creates #pragma once directives. This is even available for other compilers: #pragma once so you don't break plattform cross compatibility.

What you can do, however, is to create a VS macro like this one:

Option Strict Off
Option Explicit Off
Imports System

Public Module HeaderGuard
    Sub InsertHeaderGuard()
        Dim filename As String = DTE.ActiveDocument.Name
        filename = filename.ToUpper().Replace("."c, "_"c)
        DTE.ActiveDocument.Selection.Text = "#ifndef " + filename
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "#define " + filename
        DTE.ActiveDocument.Selection.NewLine(2)
        DTE.ActiveDocument.Selection.Text = "#endif /* " + filename + " */"
        DTE.ActiveDocument.Selection.NewLine()
    End Sub
End Module
Share:
10,546
txt
Author by

txt

Updated on June 08, 2022

Comments

  • txt
    txt almost 2 years

    I want to automatically insert include guards into newly created header files in Visual Studio 2012. Is there any predefined snippet for this purpose?

    EDIT: I'm aware of #pragma once and its wide support by compilers. But our coding style forces me to use include guards.

  • Rolf Kristensen
    Rolf Kristensen almost 11 years
    Unless one is using Visual Studio 2012 (or newer), where macro support has been removed.
  • JeffRSon
    JeffRSon almost 11 years
    Well, that's not too nice. But it may be considered to make an add-on out of it: stackoverflow.com/questions/12027485/… (haven't tried that though)
  • Skizz
    Skizz almost 11 years
    @RolfKristensen: Have they really removed macro support? How dumb is that! I created loads of macros in VS2005 that did things like set up new files with #ifdef style guards, create classes and functions with comments. Add-ons are just too much effort to create compared to a macro. Does this also mean the record temporary macro is gone too? VS used to be really good but it seems to be getting worse with each release. Currently moving to emacs.
  • JeffRSon
    JeffRSon almost 11 years
    As it looks, recording macros is also gone. Didn't know this too. Currently I'm looking at Visualstudiogallery (visualstudiogallery.msdn.microsoft.com) whether there might be an extension that helps.
  • codeling
    codeling over 10 years
    ah I missed the VS 2012 part - I tried it in VS2010, and it tells me "...bound to command Surround With which is currently not available)"... so it seems they added it in between! thanks for the quick answer
  • saurabheights
    saurabheights almost 8 years
    Pragma once is default and not really relevant to question. It's already mentioned in question.
  • Rich
    Rich almost 8 years
    @saurabheights: Note that the part in the question was added after this answer ...