Automatically stop Visual C++ 2008 build at first

2019-01-03 13:46发布

I know I can compile individual source files, but sometimes -- say, when editing a header file used by many .cpp files -- multiple source files need to be recompiled. That's what Build is for.

Default behavior of the "Build" command in VC9 (Visual C++ 2008) is to attempt to compile all files that need it. Sometimes this just results in many failed compiles. I usually just watch for errors and hit ctrl-break to stop the build manually.

Is there a way to configure it such the build stops at the very first compile error (not the first failed project build) automatically?

6条回答
在下西门庆
2楼-- · 2019-01-03 14:10

This can be done by adding a macro that is run in response to the event OnBuildProjConfigDone.

The macro is as follows:

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
查看更多
Melony?
3楼-- · 2019-01-03 14:13

Yeah, this works fine on 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
查看更多
Melony?
4楼-- · 2019-01-03 14:15

I know the question was for VS 2008, but I stumbled across it when searching for the same answer for VS 2012. Since macros are no longer supported in 2012, macro solutions won't work anymore.

You can download an extension that apparently works in VS 2010 and 2012 here. I can confirm that it works well in VS 2012.

The original link to the extension was given in this response.

查看更多
Summer. ? 凉城
5楼-- · 2019-01-03 14:17

You can also download this extension, seems to work for every version of Visual Studio

查看更多
祖国的老花朵
6楼-- · 2019-01-03 14:19

There is this post - not sure if it stops the build at the first error or the first failed project in a solution.

Ctrl-break will also stop it manually.

Now if there was some way to stop it spending 10mins rebuilding intelisense after a build failed!

查看更多
Juvenile、少年°
7楼-- · 2019-01-03 14:30

I came up with a better macro guys. It stops immediately after the first error/s (soon as build window is updated).

Visual Studio -> Tools -> Macros -> Macro IDE... (or 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 

Hope it works out for you guys.

查看更多
登录 后发表回答