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:
Thanks, Visual Studio!
Visual Studio will automatically catch messages that were printed to stdout/stderr in the following format:
<origin>[(position)]: [category] <kind> <code>: <description>
- Origin (required): tool name or a file path where the problem occurred.
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
(10)for line 10) and
(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):
- 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.
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.