Emitting custom warnings/errors in Visual Studio custom build events

Custom warning and error messages in a Post-Build Event in Visual Studio

This is a small post about how to display custom warning/error messages in Pre-Build/Pre-Link/Post-Build events in Visual Studio, which consequently means that you can have build event errors look better than this:

A default error message about a Post-Build Event in Visual Studio, which simply quotes the entire build command.
Thanks, Visual Studio!

The anatomy of an error message

Visual Studio will automatically catch messages that were printed to stdout/stderr in the following format:

<origin>[(position)]: [category] <kind> <code>: <description>

Where:

  • Origin (required): tool name or a file path where the problem occurred.
    Examples: myTool, C:\MyProject\file.cpp
  • Position (optional): if origin is a file path, can be used to mark the row/column, which in turn result in the IDE moving the cart accordingly when double-clicking on an error/warning to navigate to the file.

    Supported formats are (row) (e.g. (10) for line 10) and (row,column) (e.g. (10,3) for col 3 on line 10). Rows and columns are 1-based, much like seen in the IDE.

    Although a 2006 article described a handful of range-based formats, none of these seem to work as of VS2019 and the editor will not select a text range when using them.

  • Category (optional): when origin is a file path, this is commonly used to describe where the error is coming from, such as C++ Code Analysis. Do note, however, that Category column is not enabled by default in the Error List table as of VS2019, so avoid communicating critical information in this field.
  • Kind (required): warning, error, or note (case-insensitive).
  • Code (required): error code, which may contain anything but spaces.
    It is not clear as to whether there are specific expectations for error codes used for user-defined actions.
  • Description (required): the rest of the message will be considered the description of the error and displayed accordingly. This can be localized for convenience.

Examples

The two lines from the opening screenshot in this post were produced by adding the following to the Post-Build Event:

echo File: warning Code: Oh
echo File(2): error Code: Oh no

Adding a category would have these like so:

echo File: General warning Code: Oh
echo File(2): General error Code: Oh no

An error with position information:

echo $(ProjectDir)$(ProjectName).cpp(4, 2): error 0: a full-path error!

Note that you can also invoke scripts from build events (see documentation), allowing to split up the logic instead of cramming everything into the plaintext Command Line box.


That's all!

Related posts:

3 thoughts on “Emitting custom warnings/errors in Visual Studio custom build events

  1. The “Messages” (info) type of output can be pushed by using the (undocumented) tag of Kind = “note”.

    Examples:

    echo File(2): General note Code: Oh I see!

    echo $(ProjectDir)$(ProjectName).cpp(4, 2): note 0: a full-path info Message!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.