错误 MSB4095:如何在 Visual Studio 项目属性对话框中正确添加其他设置?

error MSB4095: How to properly add additional settings in Visual Studio project properties dialog?

我正在尝试向项目添加额外的配置项,目的是创建一个用户友好的 Dlib 项目来编译和 link Dlib。

这是我的 .xaml 文件,基于我在 visual studio 安装目录中找到的内容:

<?xml version="1.0" encoding="utf-8"?>
<ProjectSchemaDefinitions xmlns="clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework">
  <Rule Name="DLibOptionsRule" PageTemplate="generic" DisplayName="DLib settings">
    <Rule.Categories>
      <Category Name="dlib" DisplayName="DLib" />
    </Rule.Categories>
    <Rule.DataSource>
      <DataSource Persistence="ProjectFile" ItemType="" />
    </Rule.DataSource>
    <BoolProperty Name="DlibCUDAEnabled" DisplayName="Use CUDA" Default="false" Category="dlib" IncludeInCommandLine="false"/>
    <StringProperty Name="DlibCUDAPath" DisplayName="CUDA path" Description="Path to the CUDA library" Category="dlib" IncludeInCommandLine="false">
      <StringProperty.ValueEditors>
        <ValueEditor EditorType="DefaultStringPropertyEditor" DisplayName="&lt;Edit...&gt;"/>
        <ValueEditor EditorType="DefaultFilePropertyEditor" DisplayName="&lt;Browse...&gt;"/>
      </StringProperty.ValueEditors>
    </StringProperty>
  </Rule>
</ProjectSchemaDefinitions>

然后我将其包含在此 .targets 文件中:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <PropertyPageSchema Include="$(MSBuildThisFileDirectory)\DlibOptions.xaml" />
  </ItemGroup>

  <Target Name="DlibSetDefines" BeforeTargets="PreBuildEvent">
    <Message Text="Setting DLIB_USE_CUDA=%(DlibCUDAEnabled)" />
    <ItemGroup>
      <ClCompile>
        <PreprocessorDefinitions>DLIB_USE_CUDA=%(DlibCUDAEnabled);%(PreprocessorDefinitions)</PreprocessorDefinitions>
      </ClCompile>
    </ItemGroup>
  </Target>
</Project>

最后,我将目标包括在 Visual Studio 项目结束部分的正上方:

  <Import Project="DLibOptions.targets" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

我可以在项目属性对话框中看到配置项:

但是,当我尝试构建时,出现此错误:

error MSB4095: The item metadata %(DlibCUDAEnabled) is being referenced without an item name.  Specify the item name by using %(itemname.DlibCUDAEnabled).

如果你愿意看,可以在这里找到完整的项目:https://github.com/Darker/dlib-vs-solution

问题是

<Message Text="Setting DLIB_USE_CUDA=%(DlibCUDAEnabled)" />

永远无法工作,因为它没有指定要显示的项目,因此 Message 任务不知道要循环什么。您可能打算:

<Message Text="Setting DLIB_USE_CUDA=%(ClCompile.DlibCUDAEnabled)" />

但这不是很有用,因为它只会列出标志。这更有趣并显示标志 + 源文件:

<Message Text="Setting DLIB_USE_CUDA=%(ClCompile.DlibCUDAEnabled) for %(ClCompile.Identity)" />