简体   繁体   English

VS2017:如何在不破坏我设置的过滤器的情况下在.vcxproj中编写每个文件选项?

[英]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. 我学会了如何编辑我的.vcxproj文件来编译/ Za和一些没有的文件。

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. 但是看起来,因为我在.vcxproj文件中添加元素,所以我在IDE中设置的过滤器搞砸了,而.vcxproj.filters文件变得更加混乱,三重,四重,不工作每次保存时写入更多重复项的重复项。

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? 如何设置编译器选项(如/ Za)以自动应用于所有新文件,但不是某些旧文件,并且仍然有过滤器正常工作?

Here is a condensed example of what I added to my .vcxproj file: 这是我添加到.vcxproj文件中的简要示例:

  <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. 奇怪的是,这会将所有.h文件放在正确的过滤器中,但是所有过滤器之外的项目根目录下的所有cpp / c文件。

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. 我在IDE或.vcxproj.filters文件中所做的任何事情都不会产生差异,这不仅在下次项目保存时被覆盖。

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. 当所有文件都单独包含在内而不使用globbing **\\*.cpp参数时,控制项目要容易得多。 I doubt that using attribute Exclude="BufferTrio.cpp;GraphicsFacade.cpp;" 我怀疑使用属性Exclude="BufferTrio.cpp;GraphicsFacade.cpp;" jointly with Include="**\\*.cpp;" Include="**\\*.cpp;" on MSBuild property is correctly parsed and used. 在MSBuild属性上正确解析和使用。

Secondly it is best to create two <ItemGroup></ItemGroup> nodes to control independently new and old files and apply to them different compiler flags. 其次,最好创建两个<ItemGroup></ItemGroup>节点来独立控制新旧文件,并向它们应用不同的编译器标志。

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: 第三,您可以创建显式的<project-name>.vcxproj.filters项目文件,其中可以使用一个文件粒度为源和头定义过滤,即:

<?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 最后,可以创建构建任务,该任务将根据文件创建或提交到repo的日期划分文件,并根据比较结果将其应用于编译器标记,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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