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

Time:09-22

I am trying to add additional config items to a project, with the aim to create an user-friendly Dlib project to compile and link Dlib.

This is my .xaml file, based on stuff I found in the visual studio install directory:

<?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>

I then include that in this .targets file:

<?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>

And finally, I include the targets just above the ending section of a Visual Studio project:

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

I can see the config items in the project properties dialog:

enter image description here

However, when I try to build, I get this error:

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

The full project can be found here if you'd be willing to take a look: https://github.com/Darker/dlib-vs-solution

CodePudding user response:

Problem is

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

can never work because it doesn't specify which Item to show so the Message task doesn't know what to loop over. You probably intended:

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

but that's not super useful as it will just list the flag. This is more interesting and shows flag srource file:

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