MSBuild在构建VC ++项目时覆盖属性

时间:2016-08-09 00:27:31

标签: properties msbuild

我试图通过MSBuild上的命令行来构建具有不同属性的vcxproj。我测试了WarningLevel属性,因为它在MSDN MSBuild介绍页面上。

我的项目最初将WarningLevel设置为3.我开始了:

FILE *numbers = fopen("./e13.txt", "r");

//seeking the end of the file to get the correct size for the string
//I will store
fseek(numbers, 0, SEEK_END);
long fsize = ftell(numbers);
fseek(numbers, 0, SEEK_SET);

//Allocating memory to the string
char *string = malloc(fsize + 1);

我检查了跟踪器日志,看看执行命令是什么,结果是" / W3"除了" / W4"正如所料。然而," OutDir"设置正确,我可以在bin \ Debug目录中找到目标文件。

我使用WarningLevel做了什么错误吗?如何正确覆盖属性?请以正确的方式教我,我将非常感激。

此致

SL

1 个答案:

答案 0 :(得分:3)

这实际上是this question的副本,但是那个是用于将项添加到PreProcessorDefinitions列表中,而这些项不是与覆盖WarningLevel完全相同,它只能有一个值。所以它是这样的:WarningLevel不是像OutDir那样的全局属性,但它是ClCompile ItemDefinitionGroup的一部分,所以你不能直接设置它。在项目文件中看起来有点像这样:

<PropertyGroup>
  <!-- global property, can be overridden on commandline -->
  <OutDir>$(ProjectDir)$(Configuration)$(Platform)</OutDir>
</PropertyGroup>

<ItemDefinitionGroup>
  <ClCompile>
    <!-- not a global property -->
    <WarningLevel>Level4</WarningLevel>
  </ClCompile>
<ItemDefinitionGroup>

处理此问题的两种方法,请参阅相关问题的答案:

第一个选项是将WarningLevel设置为另一个属性,然后在命令行上定义该属性。转到项目属性 - &gt;配置属性 - &gt; C / C ++ - &gt;常规 - &gt;警告级别并输入$(MyWarningLevel)。在项目文件中,这看起来像

<ItemDefinitionGroup>
  <ClCompile>
    <!-- gets it's value from a global property now -->
    <WarningLevel>$(MyWarningLevel)</WarningLevel>
  </ClCompile>
<ItemDefinitionGroup>

并使用msbuild myproj.vcxproj /p:MyWarningLevel=Level3进行设置。如果未明确设置,则将使用默认值。

第二个选项是使用msbuild / C ++扩展点之一。创建一个名为override.props的文件,其中包含

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup $(PUT_IN_YOUR_CONDITION)>
    <ClCompile>
      <WarningLevel>Level4</WarningLevel>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

并让msbuild通过msbuild myproj.vcxproj /p:ForceImportBeforeCppTargets=/path/to/override.props

选择它