如何在MSBuild中定制c++编译器错误信息

How to customize C++ compiler error message(s) in MSBuild?

本文关键字:c++ 编译器 错误 信息 MSBuild      更新时间:2023-10-16

是否有可能影响MSBuild格式和报告作为构建c++项目一部分运行的CL.EXE的输出?

在命令行和Visual Studio中运行时,它们实际上是不同的:

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

这似乎是一个次要的细节,但我真的,真的需要显示[项目]也从Visual Studio内部构建,因为我触发的先决条件的构建,不想把他们的错误与父项目的错误混合。

我试过伪造$(BuildingInsideVisualStudio)并观察任何属性被传递给MSBuild,但无济于事,无法将命令行错误信息样式转换为VS.强迫它的一种方法是使用任务而不是但是生成一个单独的构建过程在任何情况下都是不可取的。有什么办法能和平地说服它吗?即使当$(BuildingInsideVisualStudio)被重写为true或false时,它似乎也不会影响错误输出的样式。奇怪。也许Visual Studio会拦截并重新格式化消息?

谢谢你的帮助

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

  1. 下创建一个名为custom.cpp.win32.targets的文件

    C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V110PlatformsWin32ImportBefore

  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>