在 Visual Studio 中插入包含防护的快速方法

问题描述 投票:0回答:5

我想自动将 include Guards 插入到 Visual Studio 2012 中新创建的头文件中。是否有任何预定义的代码片段用于此目的?

编辑:我知道 #pragma Once 及其编译器的广泛支持。但我们的编码风格迫使我使用包含防护。

c++ visual-studio visual-studio-2012
5个回答
8
投票

在 Visual Studio 2012 中使用组合键

Ctrl+K,Ctrl+S

它允许您用代码片段包围选定的代码,例如:

#if
#ifdef
#ifndef
if
class
do
enum
等等

..或指定您自己的: 演练:创建代码片段


1
投票

#pragma once

但是不,我不知道有什么可以自动为您插入

#ifndef
等。


1
投票

由于您正在标记 C++,因此您应该通过内置向导添加类。该向导创建#pragma Once 指令。这甚至适用于其他编译器:#pragma Once,这样您就不会破坏平台交叉兼容性。

但是,您可以做的是创建一个像这样的 VS 宏:

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

1
投票

如果您有 Visual Assist X,则可以删除

#pragma once
(如果存在),选择其余文本,右键单击并
Surround with (VA)
=>
#ifdef guard in a header
。如果您不喜欢默认设置,可以覆盖它,进入
VASSISTX
菜单并选择
Tools
=>
Edit VA Snippets...


0
投票

您可以使用像autohotkey这样的工具。这是一个类似问题的答案

© www.soinside.com 2019 - 2024. All rights reserved.