简体   繁体   中英

VS2017: How can I write per-file options in .vcxproj, without breaking the filters I've set up?

I learnt how to edit my .vcxproj file to compile some files with /Za and some without.

However it appears that since I'm adding elements to the .vcxproj file, the filters I have set up in the IDE are getting messed up, while the .vcxproj.filters file is turning into a much bigger mess of triple, quadruple, unworking duplicates that writes further duplicates every time it saves.

How can I set a compiler option (like /Za) to automatically apply to all new files, but not some old files, and still have filters working properly?

Here is a condensed example of what I added to my .vcxproj file:

  <ItemGroup>
    <CLCompile Include="**\*.cpp;" Exclude="BufferTrio.cpp;GraphicsFacade.cpp;">
      <AdditionalOptions>/Za %(AdditionalOptions)</AdditionalOptions>
    </CLCompile>
    <CLCompile Include="BufferTrio.cpp;GraphicsFacade.cpp;">
      <AdditionalOptions>%(AdditionalOptions)</AdditionalOptions>
    </CLCompile>
  </ItemGroup>
  <ItemGroup>
    <None Include="fragmentShader.glsl" />
    <None Include="vertexShader.glsl" />
  </ItemGroup>
  <ItemGroup>
    <Text Include="Notes.txt" />
    <Text Include="Todo.txt" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="BufferTrio.h" />
    <ClInclude Include="Exceptions.h" />
    <ClInclude Include="FileUtils.h" />
    <ClInclude Include="GraphicsFacade.h" />
  </ItemGroup>

This, oddly, puts all .h files in the right filters, but all cpp/c files at the project's root, outside all filters.

Nothing I do in the IDE or with the .vcxproj.filters file makes a difference that isn't just overwritten next time the project saves.

There are two problems which may be a cause of this behavior.

Firstly I would avoid using following pattern when you need a fine grained control over project build:

    <CLCompile Include="**\*.cpp;" Exclude="BufferTrio.cpp;GraphicsFacade.cpp;">
      <AdditionalOptions>/Za %(AdditionalOptions)</AdditionalOptions>
    </CLCompile>
    <CLCompile Include="BufferTrio.cpp;GraphicsFacade.cpp;">
      <AdditionalOptions>%(AdditionalOptions)</AdditionalOptions>
    </CLCompile>

It is much easier to control project when all files are included each one individually without the use of globbing **\\*.cpp arguments. I doubt that using attribute Exclude="BufferTrio.cpp;GraphicsFacade.cpp;" jointly with Include="**\\*.cpp;" on MSBuild property is correctly parsed and used.

Secondly it is best to create two <ItemGroup></ItemGroup> nodes to control independently new and old files and apply to them different compiler flags.

Thirdly you can create explicit <project-name>.vcxproj.filters project file in which filtering can be defined for both sources and headers with one file granularity ie:

<?xml version="1.0" encoding="UTF-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ClCompile Include="E:\src\ms\dotnet\coreclr-4c\src\jit\alloc.cpp">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="E:\src\ms\dotnet\coreclr-4c\src\jit\assertionprop.cpp">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="E:\src\ms\dotnet\coreclr-4c\src\jit\bitset.cpp">
      <Filter>Source Files</Filter>
    </ClCompile>
  </ItemGroup>
  <ItemGroup>
    <Filter Include="Source Files">
      <UniqueIdentifier>{3E79A5A2-A53A-3F44-8869-13CB1954DF36}</UniqueIdentifier>
    </Filter>
  </ItemGroup>
</Project>

Finally, it is possible to create build task which would divide files based on the date they were created or committed to repo and which would apply to them compiler flag based on the result of comparison ie

<CLCompile Include="BufferTrio.cpp;">
  <AdditionalOptions Condition="$(BufferTrioCppCreatedDate) >= $(NumericDateTimeFlagThreshold)">%(AdditionalOptions)</AdditionalOptions>
</CLCompile>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM