如何将 PreProcessorDefinitions 设置为 msbuild 任务的任务属性 [英] How to set PreProcessorDefinitions as a task propery for the msbuild task

查看:15
本文介绍了如何将 PreProcessorDefinitions 设置为 msbuild 任务的任务属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容摘自一个常规的 VS2010 C++ 项目.

The following is taken from a regular VS2010 C++ project.

    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <PrecompiledHeader>Use</PrecompiledHeader>
      <Optimization>MaxSpeed</Optimization>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>

如果我编辑 PreprocessorDefinitions 我可以设置预处理器使用的定义.我可以通过 #ifdef 等在我的代码中看到这一点.

If i edit PreprocessorDefinitions i can set a definition that is used by the preprocessor. I can see this in my code via #ifdef etc.

但是,如果我使用以下内容

However if i use the following

  <Target Name="NormalBuild" Condition=" '$(_InvalidConfigurationWarning)' != 'true' " DependsOnTargets="_DetermineManagedStateFromCL;CustomBeforeBuild;$(BuildDependsOn)" Returns="@(ManagedTargetPath)">
    <ItemGroup>
        <ManagedTargetPath Include="$(TargetPath)" Condition="'$(ManagedAssembly)' == 'true'" />
    </ItemGroup>
      <Message Text="PreprocessorDefinitions: $(PreprocessorDefinitions)" Importance="High" />
  </Target>

  <Target Name="TestBuild" Returns="@(ManagedTargetPath)">
    <MSBuild Projects="demo.vcxproj" Targets="NormalBuild" Properties="PreprocessorDefinitions=THISGETSSETBUTDOESNOTHING"/>
  </Target>

我还可以通过消息看到 PreprocessorDefinitions 包含我通过 Properties="PreprocessorDefinitions=THISGETSSETBUTDOESNOTHING" 设置的值,但我无法使用 控制我的构建#ifdef 等如果我使用常规设置并尝试使用 <Message Text="PreprocessorDefinitions: $(PreprocessorDefinitions)" 输出 PreprocessorDefinitions 该字段实际上是空白的并且不包含预期的<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> 虽然我可以使用其中任何一个键来控制我的构建使用 #ifdef 等.

i can also see via the message that PreprocessorDefinitions contains the value i set via Properties="PreprocessorDefinitions=THISGETSSETBUTDOESNOTHING" but i can not control my build using #ifdef etc. If i use a regular setup and try to output PreprocessorDefinitions using <Message Text="PreprocessorDefinitions: $(PreprocessorDefinitions)" the field is actually blank and does not contain the expected <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> although i can use any one of those keys to control my build using #ifdef etc.

  1. 这是为什么呢?
  2. 如何通过任务 Properties 元素将 PreprocessorDefinitions 传递给 VS2010 C++ 项目?
  1. Why is that?
  2. What can i do to pass PreprocessorDefinitions für a VS2010 C++ Project via the tasks Properties element?

推荐答案

你不能不修改 demo.vcxproj 因为你需要访问 PreprocessorDefinitionsCLCompile,它不是 PropertyGroup,因此无法通过 MSBuild 命令行传递.

You can't do this without modifying demo.vcxproj because you need access to the PreprocessorDefinitions of CLCompile, which is not a PropertyGroup, and thus can't be passed via the MSBuild command line.

您可以通过Project Properties -> Configuration Propertis -> C/C++ -> Preprocessor在GUI中修改预处理器定义,或直接编辑XML:

You can modify the preprocessor definitions in the GUI via Project Properties -> Configuration Propertis -> C/C++ -> Preprocessor, or edit the XML directly:

  <ClCompile>
    ....
    <PreprocessorDefinitions>$(MyMacro);%(PreprocessorDefinitions)</PreprocessorDefinitions>
  </ClCompile>

在您的 MSBuild 项目中:

In your MSBuild project:

  <Target Name="TestBuild" Returns="@(ManagedTargetPath)">
    <MSBuild Projects="demo.vcxproj" Targets="NormalBuild" Properties="MyMacro=THISGETSSETBUTDOESNOTHING"/>
  </Target>

这相当于将 MSBuild.exe 运行为:

This is equivalent to running MSBuild.exe as:

  MSBuild demo.vcxproj /p:MyMacro=THISGETSSETBUTDOESNOTHING

这篇关于如何将 PreProcessorDefinitions 设置为 msbuild 任务的任务属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆