10

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.

user10101
  • 1,704
  • 2
  • 20
  • 49

5 Answers5

8

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

Enigma
  • 1,699
  • 10
  • 14
  • 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 – codeling Sep 13 '13 at 08:21
1

#pragma once?

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

Joey
  • 344,408
  • 85
  • 689
  • 683
1

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
JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • 1
    Unless one is using Visual Studio 2012 (or newer), where macro support has been removed. – Rolf Kristensen Jun 07 '13 at 08:17
  • Well, that's not too nice. But it may be considered to make an add-on out of it: http://stackoverflow.com/questions/12027485/alternative-to-macros-in-visual-studio-2012 (haven't tried that though) – JeffRSon Jun 07 '13 at 08:24
  • @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. – Skizz Jun 07 '13 at 08:27
  • As it looks, recording macros is also gone. Didn't know this too. Currently I'm looking at Visualstudiogallery (http://visualstudiogallery.msdn.microsoft.com/) whether there might be an extension that helps. – JeffRSon Jun 07 '13 at 08:46
1

If you have Visual Assist X you can remove your #pragma once if present, select the rest of the text, right click and Surround with (VA) => #ifdef guard in a header. If you don't like the default you can override it going to VASSISTX menu and selecting Tools => Edit VA Snippets...

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48
0

You could use a tool like autohotkey. Here is an answer to an simular question.

Community
  • 1
  • 1
Jan Herrmann
  • 2,717
  • 17
  • 21