How to build or clean a specific Visual C++ project without building other dependencies in the solution (318651)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ 2005 Express Edition

This article was previously published under Q318651

SUMMARY

In a Visual C++ .NET or Visual C++ 2005 solution with multiple dependent projects, when you attempt to build or clean a specific project, you build or clean all of the projects in that solution. This step-by-step article demonstrates how to create a macro to customize the Visual C++ build environment to build only a specific project.

NOTE: This macro is applicable only to Visual C++ solutions.

back to the top

Default Behavior

To demonstrate the default behavior when you build a project that has dependencies, follow these steps:
  1. Create a new C++ application named "Consumer".
  2. Create another new C++ application named "Dependency1", and then add this application to the solution.
  3. In the Solution Explorer, click the new solution, and then on the Project menu, click Project Dependencies.
  4. Click to select Consumer from the Project drop-down list.
  5. In the Depends on list, click Dependency1. Click OK to close the Dependencies dialog box.
  6. On the Build menu, click Build Solution.
  7. Make a change to the Consumer project and build it by using either of the methods mentioned in the previous steps. Note that Visual C++ .NET and Visual C++ 2005 attempt to build both the Consumer project and the Dependency1 project, even though you changed only Consumer.
back to the top

Create a Macro to Build Only a Specific Project

The following macro enables you to build or clean a specific project in a project solution that has multiple dependencies:
Imports EnvDTE
Imports System.Diagnostics
Imports VSLangProj
Imports Microsoft.VisualStudio.VCProjectEngine
Public Module Module1
    Sub BuildSelection()
        ' This works on only the one Visual C++ project that you select in the Solution Explorer.
        Dim cppkind As String = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
        Dim config As SolutionConfiguration = DTE.Solution.SolutionBuild.ActiveConfiguration
        Dim projs As System.Array
        Dim proj As Project
        Dim vcprj As VCProject
        Dim vcconfig As VCConfiguration
        ' Which project is selected?
        projs = DTE.ActiveSolutionProjects()
        If projs.Length = 1 Then
            proj = CType(projs.GetValue(0), EnvDTE.Project)
            ' make sure we have a C++ project
            If proj.Kind() <> cppkind Then
                MsgBox("Must be a VC++ Project")
                GoTo done
            End If
            ' Th application must work directly off the VC Project Object for this.
            vcprj = proj.Object()
            vcconfig = vcprj.Configurations(config.Name() + "|Win32")
            ' Build the project.
            vcconfig.Build()
        Else
            MsgBox("Single VC Project must be selected")
        End If
done:
    End Sub
    Sub CleanSelection()
        Dim cppkind As String = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
        Dim config As SolutionConfiguration = DTE.Solution.SolutionBuild.ActiveConfiguration
        Dim projs As System.Array
        Dim proj As Project
        Dim vcprj As VCProject
        Dim vcconfig As VCConfiguration
        projs = DTE.ActiveSolutionProjects()
        If projs.Length = 1 Then
            proj = CType(projs.GetValue(0), EnvDTE.Project)
            If proj.Kind() <> cppkind Then
                MsgBox("Must be a VC++ Project")
                GoTo done
            End If
            vcprj = proj.Object()
            vcconfig = vcprj.Configurations(config.Name() + "|Win32")
            vcconfig.Clean()
        Else
            MsgBox("Single VC Project must be selected")
        End If
done:
    End Sub
End Module
				
back to the top

Add a Macro and Bind It to a Toolbar Button

To add the preceding macro and bind it to a toolbar button, follow these steps:
  1. On the Tools menu, point to Macros, and then click Macros IDE.
  2. Right-click the MyMacros project node in the Macro IDE, and then click Add Reference.
  3. Add a reference to Microsoft.VisualStudio.VCProjectEngine.dll
  4. Expand the MyMacros project node, and then double-click Module1.
  5. Paste the preceding code in the Module1 block, save the block, and then close the Macro integrated development environment (IDE).
  6. Right-click any toolbar in the Visual Studio .NET IDE, and then click Customize.
  7. In the Categories list on the Commands tab, click Macro (near the bottom of the list, above New Menu).
  8. The two new macros are named MyMacros.Module1.BuildSelection and MyMacros.Module1.CleanSelection in the Commands list.
  9. Drag the macros from the Commands list to any toolbar.
  10. Click to select a project in the Solution Explorer, and then click the tool buttons you just added.
back to the top

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
back to the top

Modification Type:MajorLast Reviewed:1/5/2006
Keywords:kbBuilder kbHOWTOmaster kbmacro KB318651 kbAudDeveloper