VS2012 中的 BeforeBuild 和其他目标发生了什么变化? [英] What happened to BeforeBuild and other targets in VS2012?

查看:22
本文介绍了VS2012 中的 BeforeBuild 和其他目标发生了什么变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Visual Studio 2012 中的 C++ 项目中执行一些预构建步骤,但它们没有被调用(虽然我很确定相同的技术在 Visual Studio 2010 中没问题).命令行构建的行为完全相同.

I'm trying to get some pre-build steps to work in a C++ project in Visual Studio 2012 but they do not get invoked (while I'm pretty sure the same techniques were OK in Visual Studio 2010). Command line builds behave exactly the same.

这是项目文件的结尾;该文件是使用 Visual Studio 生成的,然后我只添加了最后几行:

This is the end of the project file; the file was generated using Visual Studio and then I just added the last couple of lines:

<Import Project="$(VCTargetsPath)Microsoft.Cpp.targets" />
<Target Name="BeforeBuild">
  <Message Text="### BeforeBuild ###" />
</Target>
<Target Name="BeforeCompile">
  <Message Text="### BeforeCompile ###" />
</Target>
<Target Name="AfterBuild">
  <Message Text="### AfterBuild ###" />
</Target>

这是输出:

Project "d:	emp	emp.vcxproj" on node 1 (default targets).
InitializeBuildStatus:
  Creating "Debug	emp.unsuccessfulbuild" because "AlwaysCreate" was specified.
AfterBuild:
  AfterBuild
FinalizeBuildStatus:
  Deleting file "Debug	emp.unsuccessfulbuild".
  Touching "Debug	emp.lastbuildstate".

所以只考虑 AfterBuild,忽略其他.调查这个我在 Microsoft.BuildSteps.targets 中找到了这个 PropertyGroup:

So only AfterBuild is considered and the others are ignored. Looking into this I found this PropertyGroup in Microsoft.BuildSteps.targets:

<BuildDependsOn>
  _PrepareForBuild;
  $(BuildSteps);
  AfterBuild;
  FinalizeBuildStatus;
</BuildDependsOn>

这不应该也有 BeforeBuild 和 BuildEvent 目标吗?还是我的 MSBuild 安装有问题导致它使用这个 BuildSteps.targets 文件而不是其他文件?

Shouldn't this also have BeforeBuild and the BuildEvent targets? Or is something wrong with my MSBuild install causing it to use this BuildSteps.targets file instead of something else?

解决方案

正如 Alexey 指出的那样,使用 Before/AfterTarget 提供了一种可用的解决方法.您只需要注意要使用的目标,但是通过查看 BuildSteps 文件很容易做到这一点.这现在似乎工作正常:

As Alexey points out, using Before/AfterTarget provides a usable workaround. You just have to take care of which targets to use, but this is easy by looking at the BuildSteps file. This seems to work fine for now:

<Target Name="BeforeBuild" BeforeTargets="PrepareForBuild">
  <Message Text="### BeforeBuild ###" />
</Target>
<Target Name="BeforeCompile" BeforeTargets="BuildCompile">
  <Message Text="### BeforeCompile ###" />
</Target>
<Target Name="AfterBuild" AfterTargets="Build">
  <Message Text="### AfterBuild ###" />
</Target>

推荐答案

我的 msbuild 目标和你描述的一样,所以我认为你的 msbuild 安装没问题.

I have same msbuild targets as you described, so I think your msbuild installation is fine.

看起来他们决定对目标和依赖项进行一些清理(不确定这个问题是否与 VS 版本有关,VS 只是使用相同的目标,由 msbuild 提供).BeforeBuild 和其他目标仍然存在于 Microsoft.common.targets 中.我想它只是为 .NET 项目保留的(我从未玩过 C++ 项目,所以我不知道如何在那里构建管道).

Looks like they decide to make some cleanup to targets and dependencies ( not sure if this issue related to VS version, VS just using same targets, provided by msbuild). BeforeBuild and other targets still exists in Microsoft.common.targets. I suppose it just reserved for .NET projects (I never played with C++ ones, so I don't know, how to build a pipeline there).

不管它是否适用于以前的版本,您的问题都可以更容易地解决 - 只需为 MSBuild 4.0 使用新属性 BeforeTargetsAfterTargets 并将您的目标直接挂钩到您想要的任何内容上:

Anyway whether it works or not on previous versions, your problem can be solved much easier - just use new attributes BeforeTargetsAfterTargets for MSBuild 4.0 and hook your targets directly on whatever you want:

<Import Project="$(VCTargetsPath)Microsoft.Cpp.targets" />
<Target Name="BeforeBuild" BeforeTargets="Build">
     <Message Text="### BeforeBuild ###" Importance="high" />
</Target>
<Target Name="BeforeCompile" BeforeTargets="Compile">
    <Message Text="### BeforeCompile ###" Importance="high" />
</Target>
<Target Name="AfterBuild" AfterTargets="Build">
    <Message Text="### AfterBuild ###" Importance="high" />
</Target>

这篇关于VS2012 中的 BeforeBuild 和其他目标发生了什么变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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