【问题标题】:How do I add a custom build target to a Visual C++ 2010 project?如何将自定义构建目标添加到 Visual C++ 2010 项目?
【发布时间】:2010-07-16 23:55:06
【问题描述】:

有很多指南可以帮助您在 VS2010 中使用 MSBuild 模拟 VS2008 的“自定义构建步骤”。但是,我希望我的构建更智能并使用 MSBuild。我写了a little MSBuild task,它调用了ANTLR 解析器生成器。当我在一个简单的测试 MSBuild 文件中运行该构建任务时,它可以完美运行。但是,当我尝试将我的任务添加到 C++ 项目时,我遇到了问题。基本上我已经将它添加到我的项目文件的顶部(在<project> 元素之后):

  <UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
        AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
  <Target Name="BeforeBuild"
    Inputs="ConfigurationParser.g"
    Outputs="ConfigurationParserParser.h;ConfigurationParserParser.cpp;ConfigurationParserLexer.h;ConfigurationParserLexer.cpp">
    <AntlrGrammar
      AntlrLocation="$(MSBuildProjectDirectory)Antlr.jar"
      Grammar="ConfigurationParser.g"
      RenameToCpp="true" />
  </Target>

但是,我的目标在构建之前没有被调用。

如何将我的任务添加到 C++ 构建中?

【问题讨论】:

    标签: c++ visual-c++ msbuild


    【解决方案1】:

    在阅读此答案之前,您可能想看看:

    扩展 MSBuild 的旧方法,以及我的参考书中提到的方法,本质上是基于覆盖 Microsoft 提供的默认空目标。上面第二个链接中指定的新方法是定义您自己的任意目标,并使用“BeforeTargets”和“AfterTargets”属性强制您的目标在您的预期目标之前或之后运行。

    在我的具体情况下,我需要 ANTLR Grammars 任务在 CLCompile 目标之前运行,该目标实际上构建 C++ 文件,因为 ANTLR Grammars 任务构建 .cpp 文件。因此,XML 看起来像这样:

    <Project ...
      <!-- Other things put in by VS2010 ... this is the bottom of the file -->
      <UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
      AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
        <Target Name="AntlrGrammars"
          Inputs="Configuration.g"
          Outputs="ConfigurationParser.h;ConfigurationParser.cpp;ConfigurationLexer.h;ConfigurationLexer.cpp"
          BeforeTargets="ClCompile">
          <AntlrGrammar
            AntlrLocation="$(MSBuildProjectDirectory)\Antlr.jar"
            Grammar="Configuration.g"
            RenameToCpp="true" />
        </Target>
      <ImportGroup Label="ExtensionTargets">
      </ImportGroup>
      <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    </Project>
    

    至于为什么这优于 PreBuildEvent 和/或 PostBuildEvent;当语法本身没有更新时,这很聪明,不会重建 .cpps。你会得到类似的东西:

    1>Antlr语法: 1>跳过目标“AntlrGrammars”,因为所有输出文件相对于输入文件都是最新的。 1>Cl编译: 1> 所有输出都是最新的。 1> 所有输出都是最新的。

    这也消除了 Visual Studio 每次运行程序时不断抱怨它需要重建事物的声音,就像它使用简单的构建前和构建后步骤一样。

    希望这对某人有所帮助 - 让我永远搞砸了。

    【讨论】:

    • 谢谢!这也需要我永远弄清楚。
    猜你喜欢
    • 2013-06-10
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多