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

查看:116
本文介绍了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:\temp\temp.vcxproj" on node 1 (default targets).
InitializeBuildStatus:
  Creating "Debug\temp.unsuccessfulbuild" because "AlwaysCreate" was specified.
AfterBuild:
  AfterBuild
FinalizeBuildStatus:
  Deleting file "Debug\temp.unsuccessfulbuild".
  Touching "Debug\temp.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安装很好. 看起来他们决定对目标和依赖项进行一些清理(不确定此问题是否与VS版本有关,VS是否仅使用msbuild提供的相同目标).在Microsoft.common.targets中,BeforeBuild和其他目标仍然存在.我想它只是为.NET项目保留的(我从没玩过C ++项目,所以我不在那里建立管道).

I have same msbuild targets as you described, so I think your msbuild installation is fine. 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 dunno build pipeline there).

无论它在以前的版本中是否正常运行,都可以轻松解决您的问题-只需使用MSBuild 4.0的新属性BeforeTargets \ AfterTargets并将目标直接挂接到您想要的任何对象上即可:

Anyway whether it works or not on previous versions, your problem can be solved much easier - just use new attributes BeforeTargets\AfterTargets 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天全站免登陆