visual-studio - VS 不遵守 PropertyGroup 配置设置

标签 visual-studio msbuild projects-and-solutions

我正在尝试压缩 XML 项目文件。该项目编译单个源文件,但由于喜欢多次复制相同的设置,因此其大小超过 11 KB。我正在尝试移动每个源文件的设置,并将它们提升到具有不同条件语句或表达式的配置 PropertyGroup 中(XML 如何调用它们?)。

如果我正在解析A guide to .vcxproj and .props file structure正确地从 MSDN 博客中,我们可以将设置放在属性组中:

<PropertyGroup Label=“Configuration“ />

  <!– This property sheet (directly or via imports) defines the

  default values for many tool-specific properties such as the

  compiler’s Optimization, WarningLevel properties, Midl tool’s

  TypeLibraryName property, etc...

但是,当我通过关闭 VS 然后使用修改后的项目文件重新打开它来检查我的修改时,没有选择任何设置。 Visual Studio 似乎正在使用自己的设置。

我有几个问题:

  1. 是否有 Visual Studio 设置告诉它停止将每个设置喷洒到每个源文件中?没有理由为每个配置中的每个源文件重复设置 175x4 次。

  2. Visual Studio 是否有压缩实用程序,这样我就不必手动执行此操作?这是非常乏味的工作。

  3. 为什么 Visual Studio 没有获取设置?我该如何解决这个问题?


这是包含预处理器宏的属性页的屏幕截图。修改后的XML有以下内容,在Visual Studio中没有反射(reflect)。我尝试过使用和不使用 ClCompile .

<!-- Debug Configurations -->
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Release Configuration">
  <ClCompile>  
    <PreprocessorDefinitions>CRYPTOPP_DLL_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    ...
  </ClCompile>    
</PropertyGroup>

enter image description here


这是压缩的项目文件。 original can be found here 。该项目编译了1个源文件,未压缩的大小超过11KB。

我的模组从 <!-- All Configurations --> 开始,并结束于 <!-- Back to Visual Studio boilerplate --> 。我还没有完成所有设置的提升,但是已经完成的设置,例如预处理器宏和运行时库,没有反射(reflect)在 Visual Studio 下。

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{1974a53a-9863-41c9-886d-b2b8c2fc3c8b}</ProjectGuid>
    <RootNamespace>dlltest</RootNamespace>
    <PlatformToolset>v100</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

  <!-- All Configurations -->
  <PropertyGroup Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseOfMfc>false</UseOfMfc>
    <CharacterSet>MultiByte</CharacterSet>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <OutDir>$(Platform)\DLL_Output\$(Configuration)\</OutDir>
    <IntDir>$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
    <ClCompile>
      <WarningLevel>Level4</WarningLevel>
      <CallingConvention>StdCall</CallingConvention>
      <SuppressStartupBanner>true</SuppressStartupBanner>
    </ClCompile>      
  </PropertyGroup>

  <!-- Debug Configurations -->
  <PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Debug Configuration">
    <ClCompile>  
      <PreprocessorDefinitions>CRYPTOPP_DLL_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <Optimization>Disabled</Optimization>
      <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
    </ClCompile>      
  </PropertyGroup>

  <!-- Release Configurations -->
  <PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Release Configuration">
    <ClCompile>
      <PreprocessorDefinitions>NDEBUG;CRYPTOPP_DLL_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <OmitFramePointers>true</OmitFramePointers>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
    </ClCompile>
  </PropertyGroup>

  <!-- X86 Configurations -->
  <PropertyGroup Condition="'$(Platform)'=='Win32'" Label="X86 Configuration">
    <ClCompile>
      <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
    </ClCompile>    
  </PropertyGroup>

  <!-- X64 Configurations -->
  <PropertyGroup Condition="'$(Platform)'=='x64'" Label="X64 Configuration">
    <ClCompile>
      <!-- <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet> -->
      <EnableEnhancedInstructionSet>StreamingSIMDExtensions</EnableEnhancedInstructionSet>
    </ClCompile>    
  </PropertyGroup>

  <!-- Back to Visual Studio boilerplate -->
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <StringPooling>true</StringPooling>
      <PrecompiledHeader />
    </ClCompile>
    <ResourceCompile>
      <PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <Culture>0x0409</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies>cryptopp.lib;Ws2_32.lib</AdditionalDependencies>
      <AdditionalLibraryDirectories>$(Platform)\DLL_Output\$(Configuration)</AdditionalLibraryDirectories>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <SubSystem>Console</SubSystem>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <ClCompile>
      <StringPooling>true</StringPooling>
      <PrecompiledHeader />
    </ClCompile>
    <ResourceCompile>
      <PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <Culture>0x0409</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies>cryptopp.lib;Ws2_32.lib</AdditionalDependencies>
      <AdditionalLibraryDirectories>$(Platform)\DLL_Output\$(Configuration)</AdditionalLibraryDirectories>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <SubSystem>Console</SubSystem>
      <TargetMachine>MachineX64</TargetMachine>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <PrecompiledHeader />
      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
    </ClCompile>
    <ResourceCompile>
      <PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <Culture>0x0409</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies>cryptopp.lib;Ws2_32.lib</AdditionalDependencies>
      <AdditionalLibraryDirectories>$(Platform)\DLL_Output\$(Configuration)</AdditionalLibraryDirectories>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <SubSystem>Console</SubSystem>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile>
      <PrecompiledHeader />
      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
    </ClCompile>
    <ResourceCompile>
      <PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <Culture>0x0409</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies>cryptopp.lib;Ws2_32.lib</AdditionalDependencies>
      <AdditionalLibraryDirectories>$(Platform)\DLL_Output\$(Configuration)</AdditionalLibraryDirectories>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <SubSystem>Console</SubSystem>
      <TargetMachine>MachineX64</TargetMachine>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup>
    <ClCompile Include="dlltest.cpp" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

最佳答案

您的设置被覆盖,因为您将它们放在文件中太早。按照 VS 的做法,将它们放在 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> 之后 .

更新我的错误;乍一看,您的设置没有任何效果,因为您将它们定义为属性。像VS一样做,并在 ItemDefinitionGroup 中定义它们而不是 PropertyGroup (同样,根据 msbuild 评估规则位于正确的位置)

至于压缩:将您的设置存储在单独的属性表中并导入它们。如果组织得当,这将允许您不再需要编辑项目文件设置本身(因此 VS 填充的 ItemDefinitionGroups 将为空),但您只需根据需要添加/删除属性表,项目文件只是一个容器用于源文件/头文件和属性表。如果您在 VS gui(称为“属性管理器”)中进行导入,它们将自动位于前面提到的正确位置,并且顺序也可以。还可以通过与项目相同的选项对话框在 VS 中编辑它们。

请注意,您可以创建导入其他属性表的整个层次结构,这对于避免重复很有用。例如。您无需手动将 Ws2_32.lib 添加到每个配置/平台组合,只需导入一个属性表即可完成此操作。对于根据平台/配置等具有不同名称的库特别方便。假设您已经为编译器/链接器/...创建了一些单独的标准属性表...您可以将它们添加到“主”属性表中。示例:

enter image description here

关于visual-studio - VS 不遵守 PropertyGroup 配置设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848215/

相关文章:

c# - 确定对不正确的 .NET Framework 版本的间接依赖的来源

c# - 作为构建步骤,如何从解决方案中的项目之一运行程序?

c++ - 错误 : missing type specifier - int assumed. C++ 不支持默认 int

.net - 从packages.config 迁移到PackageReferences 导致无法加载文件或程序集 "..."或其依赖项之一。访问被拒绝

msbuild - 在 MSBuild 中传递变量的不同方法

visual-studio-2010 - 在Visual Studio 2010中同时将多个项目添加到一个解决方案中

java - IntelliJ中等效的Eclipse工作区?

visual-studio - 如何在 Visual Studio 中为局部变量和全局变量设置不同的颜色?

c# - .NET 转到 NuGet 包的实现

MSBuild - 如何在部署时强制 "AfterBuild"目标?