首页 > 解决方案 > 错误 MSB4095:如何在 Visual Studio 项目属性对话框中正确添加其他设置?

问题描述

我正在尝试将其他配置项添加到项目中,目的是创建一个用户友好的 Dlib 项目来编译和链接 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

标签: xamlmsbuildvisual-studio-2019

解决方案


问题是

<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)" />

推荐阅读