首次出现编译错误时自动停止 Visual C++ 2008 生成

Automatically stop Visual C++ 2008 build at first compile error?

本文关键字:Visual C++ 生成 2008 编译 错误      更新时间:2023-10-16

我知道我可以编译单个源文件,但有时 - 例如,在编辑许多.cpp文件使用的头文件时 - 需要重新编译多个源文件。 这就是构建的目的。

VC9 (Visual C++ 2008) 中"生成"命令的默认行为是尝试编译需要它的所有文件。 有时这只会导致许多编译失败。 我通常只观察错误并按 ctrl-break 手动停止构建。

有没有办法配置它,使构建在第一个编译错误(而不是第一个失败的项目构建)自动停止?

我想出了一个更好的宏家伙。它在第一个错误后立即停止(一旦生成窗口更新)。

Visual Studio -> Tools -> Macros -> Macro IDE...(或 ALT+F11)

Private Sub OutputWindowEvents_OnPaneUpdated(ByVal pPane As OutputWindowPane) Handles OutputWindowEvents.PaneUpdated
    If Not (pPane.Name = "Build") Then Exit Sub
    pPane.TextDocument.Selection.SelectAll()
    Dim Context As String = pPane.TextDocument.Selection.Text
    pPane.TextDocument.Selection.EndOfDocument()
    Dim found As Integer = Context.IndexOf(": error ")
    If found > 0 Then
        DTE.ExecuteCommand("Build.Cancel")
    End If
End Sub 

希望它对你们有用。

这可以通过添加响应事件 OnBuildProjConfigDone 而运行的宏来完成。

宏如下所示:

Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
  If Success = False Then
    DTE.ExecuteCommand("Build.Cancel")
  End If
End Sub

是的,这在 MSVC 2005-2010 上工作正常:

Public Module EnvironmentEvents
  Private Sub OutputWindowEvents_OnPaneUpdated(ByVal pPane As OutputWindowPane) Handles OutputWindowEvents.PaneUpdated
    If Not (pPane.Name = "Build") Then Exit Sub
    Dim foundError As Boolean = pPane.TextDocument.StartPoint.CreateEditPoint().FindPattern(": error")
    Dim foundFatal As Boolean = pPane.TextDocument.StartPoint.CreateEditPoint().FindPattern(": fatal error")
    If foundError Or foundFatal Then
      DTE.ExecuteCommand("Build.Cancel")
    End If
  End Sub
End Module
我知道

这个问题是针对VS 2008的,但是在为VS 2012搜索相同的答案时,我偶然发现了它。由于 2012 年不再支持宏,宏解决方案将不再有效。

您可以在此处下载显然适用于VS 2010和2012的扩展。我可以确认它在VS 2012中运行良好。

此响应中提供了指向扩展的原始链接。

有这篇文章 - 不确定它是在解决方案中的第一个错误还是第一个失败的项目时停止构建。

按 Ctrl 中断也会手动停止它。

现在,如果有某种方法可以阻止它在构建失败后花费 10 分钟重建智能!

你也可以下载这个扩展,似乎适用于每个版本的Visual Studio