48

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?

jwfearn
  • 28,781
  • 28
  • 95
  • 122
  • 2
    Dear people from [The Future](http://xkcd.com/979) - in VS 2010+, try the [StopOnFirstBuildError](http://visualstudiogallery.msdn.microsoft.com/91aaa139-5d3c-43a7-b39f-369196a84fa5) extension – brichins Feb 07 '13 at 16:20

6 Answers6

28

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.

Eric
  • 3,773
  • 3
  • 29
  • 29
  • @Eric, thanks, this looks like a good solution, I'll give it a try. Anyone know why @Eric's suggestion was down-voted? – jwfearn Jul 06 '09 at 18:50
  • 1
    According to my reputation page I wasn't down-voted. I dunno. – Eric Jul 07 '09 at 12:39
  • Good effort, and it does work. But it flickers like anything while building, so it's not for me. If only there was a way to get the text from the output window without having to do select all... – demoncodemonkey Apr 23 '10 at 14:06
  • 2
    You don't need to use select all; you should be able to do something like `bool found = pPane.TextDocument.StartPoint.CreateEditPoint().FindPattern(": error")`. It should hopefully be quicker, too, since it doesn't rely on copying the entire text of the output window out on every pane update. – Noah Richards Jun 11 '10 at 02:43
  • @Noah Richards, Thanks for weighing in! I wasn't able to find that, and so I settled for SA. Next time I fire up VS I'll give it a try and update the answer. – Eric Jun 24 '10 at 04:50
  • Let me know if it doesn't work, and I'll try to find something else (I didn't actually try it, just looked up API). It doesn't directly apply, but you can see my answer to the VS2010 version of this question here: http://stackoverflow.com/questions/3041982/vs2010-how-to-automatically-stop-compile-on-first-compile-error/3042159#3042159. – Noah Richards Jun 24 '10 at 05:01
  • This scans the whole output every update which is horribly slow. A better approach would be to update the macro to only look at each line added or some other sort of text-delta. – void.pointer Jul 16 '12 at 20:21
17

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
jmatthias
  • 7,375
  • 7
  • 27
  • 36
  • 1
    this method stops the build at the first PROJECT failure not the first compile error. – jwfearn Oct 13 '08 at 16:47
  • 4
    Yes, but for me that's close enough (as opposed to building the next 20 or so projects before stopping). – jmatthias Dec 07 '08 at 06:37
  • 1
    I guess it depends on how many files are in a project and what you're compiling. If I change a file included everywhere, it can trigger a fairly long rebuild and if I have a typo, I'd rather have my build stop right away. Perhaps VC10 (with MSBuild) will be able to do what I want. – jwfearn Dec 27 '08 at 01:55
  • 7
    This isn't what the original poster was looking for, but it's just what I was looking for. In case it's not obvious, you add this by going to Tools->Macros->Macros IDE, open EnvironmentEvents and paste it in there. – mhenry1384 Jan 13 '09 at 20:05
  • You can also add DTE.ExecuteCommand("View.ErrorList") just after the DTE.ExecuteCommand("Build.Cancel") and it will show the error list panel immediatly. – Samuel Jul 05 '11 at 14:00
  • I added a new macro under MyMacros in the VS Macro IDE. Now how do I get it to run when I build? Do I have to associate it with my VS solution somehow? – sizzle May 23 '13 at 16:35
8

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
  • 1
    Worked for me in VS 2008, thanks. One thing I added to open the error list `DTE.Windows.Item(EnvDTE80.WindowKinds.vsWindowKindErrorList).Activate()` – Thomas Oct 12 '11 at 17:44
3

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.

Community
  • 1
  • 1
RKG
  • 150
  • 2
  • 8
1

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!

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • But ctrl-break corrupts the files being created at that time, so you have to recompile them explicitly. – Lev Sep 26 '08 at 12:02
  • 2
    the method described in the link stops the build at the first PROJECT failure not the first compile error. – jwfearn Oct 13 '08 at 16:41
  • The archived link is [Visual Studio Tip: Kill that Build!](https://web.archive.org/web/20120507094056/http://stevenharman.net:80/blog/archive/2008/01/17/visual-studio-tip-kill-that-build.aspx) – zhenguoli Sep 28 '21 at 12:18
1

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

QuantumBlack
  • 1,549
  • 11
  • 27