views:

4239

answers:

3

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?

+1  A: 

There is this post http://stevenharman.net/blog/archive/2008/01/17/visual-studio-tip-kill-that-build.aspx 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 Beckett
But ctrl-break corrupts the files being created at that time, so you have to recompile them explicitly.
Lev
the method described in the link stops the build at the first PROJECT failure not the first compile error.
jwfearn
+8  A: 

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
this method stops the build at the first PROJECT failure not the first compile error.
jwfearn
Yes, but for me that's close enough (as opposed to building the next 20 or so projects before stopping).
jmatthias
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
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
+11  A: 

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 Muyser
@Eric, thanks, this looks like a good solution, I'll give it a try. Anyone know why @Eric's suggestion was down-voted?
jwfearn
According to my reputation page I wasn't down-voted. I dunno.
Eric Muyser
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
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
@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 Muyser
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