如何创建VS2008宏来改变C ++项目设置?

时间:2011-02-28 12:01:18

标签: visual-studio-2008 visual-studio-macros

我想编写一个Visual Studio宏来关闭给定C ++项目的优化。 有没有人有示例代码来改变项目设置?假设项目设置作为某种值的字典公开,项目的C ++和链接器优化的键和值是什么?

2 个答案:

答案 0 :(得分:1)

Visual C ++项目模型:

http://msdn.microsoft.com/en-us/library/2eydyk57.aspx

一些黑客攻击的宏代码:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports Microsoft.VisualStudio.VCProjectEngine
Imports System.Text


Public Module RecordingModule

    Sub DisableOptimizationsInAllProjectsReleaseWin32()
        For i = 1 To DTE.Solution.Projects.Count
            Dim proj As Object = DTE.Solution.Projects.Item(i).Object
            On Error Resume Next
            Dim prj As VCProject = CType(proj, Microsoft.VisualStudio.VCProjectEngine.VCProject)
            If Not prj Is Nothing Then
                Dim cfgs As IVCCollection = CType(prj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)

                For c = 1 To cfgs.Count
                    Dim cfg As VCConfiguration = CType(cfgs.Item(c), Microsoft.VisualStudio.VCProjectEngine.VCConfiguration)
                    If cfg.Name = "Release|Win32" Then
                        Dim tool As VCCLCompilerTool = CType(cfg.Tools("VCCLCompilerTool"), Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
                        If Not tool Is Nothing Then tool.Optimization = optimizeOption.optimizeDisabled

                        Dim linker As VCLinkerTool = CType(cfg.Tools("VCLinkerTool"), Microsoft.VisualStudio.VCProjectEngine.VCLinkerTool)
                        If Not linker Is Nothing Then
                            linker.EnableCOMDATFolding = optFoldingType.optFoldingDefault
                            linker.LinkTimeCodeGeneration = LinkTimeCodeGenerationOption.LinkTimeCodeGenerationOptionDefault
                            linker.OptimizeReferences = optRefType.optReferencesDefault
                            linker.InlineFunctionExpansion = "0"
                            linker.EnableIntrinsicFunctions = "false"
                            linker.FavorSizeOrSpeed = "0"
                            linker.WholeProgramOptimization = "false"

                        End If
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub
End Module

答案 1 :(得分:0)

来自@mackenir答案的修改代码 - 对于当前项目:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text
Imports Microsoft.VisualStudio.VCProjectEngine
' Add Reference to Microsoft.VisualStudio.VCProjectEngine: Project > Add Refrence

Public Module Build

Sub CheckBuild()

    Dim Prj As Object = DTE.ActiveSolutionProjects(0).Object
    If Prj Is Nothing Then Return
    Dim VPrj As VCProject = CType(Prj, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    If VPrj Is Nothing Then Return
    Dim Cfg As VCConfiguration = CType(VPrj.Configurations.Item("Debug|x64"), Microsoft.VisualStudio.VCProjectEngine.VCConfiguration)
    If Cfg Is Nothing Then Return
    Dim CTool As VCCLCompilerTool = CType(Cfg.Tools("VCCLCompilerTool"), Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
    If CTool Is Nothing Then Return
    Dim LTool As VCLinkerTool = CType(Cfg.Tools("VCLinkerTool"), Microsoft.VisualStudio.VCProjectEngine.VCLinkerTool)
    If LTool Is Nothing Then Return

    CTool.DebugInformationFormat = debugOption.debugDisabled
    LTool.GenerateDebugInformation = "false"
    LTool.OptimizeReferences = optRefType.optNoReferences
    LTool.EnableCOMDATFolding = optFoldingType.optNoFolding

    ExecuteCommand("Build.BuildSelection")

End Sub