如何在MSBuild中自定义C ++编译器错误消息?

时间:2014-08-01 17:08:04

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

是否有可能影响MSBuild格式化和报告CL.EXE的输出在构建C ++项目过程中的运行方式?

从命令行和Visual Studio运行时,它们实际上有所不同:

Command-line:  file(line): error message [project]
Visual Studio: file(line): error message

这似乎是一个小细节,但是我确实需要从Visual Studio内部显示[project],因为我触发了先决条件的构建,并且不希望将它们的错误与错误混合在一起。父项目。

我尝试伪造$(BuildingInsideVisualStudio)并观察传递给MSBuild的任何属性,但无效,无法将命令行错误消息样式输入VS.强制它的一种方法是使用< Exec>。任务而不是< MSBuild>但是,几乎在所有可以想象的方式中,产生单独的构建过程是不可取的。有没有办法和平地说服它?即使$(BuildingInsideVisualStudio)被重写为true或false,它似乎也不会影响错误输出的样式。奇怪。也许Visual Studio拦截并重新格式化消息?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

MSBuild提供了非常好的可扩展性。将任何* .targets文件放在$(VCTargetsPath)\Platforms\$(Platform)\ImportBefore\下,并在标准处理之前加载自定义目标。

  1. 在以下位置创建名为custom.cpp.win32.targets的文件:

      

    C:\ Program Files(x86)\ MSBuild \ Microsoft.Cpp \ v4.0 \ V110 \ Platforms \ Win32 \ ImportBefore \

  2. 将以下内容复制到custom.cpp.win32.targets。这将在调用cl.exe之前打印项目的完整路径:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <PropertyGroup>
      <BeforeClCompileTargets>
         BeforeClCompileCustom;
       $(BeforeClCompileTargets);
      </BeforeClCompileTargets> 
    </PropertyGroup>
    
    <Target Name="BeforeClCompileCustom">
        <Message Importance="high" 
                 Text="Building '$(ProjectName)' - $(Platform)|$(Configuration) [$(MSBuildProjectFullPath)]" />
    </Target>