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!
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
, ornote
(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!
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!
Thank you!
You’re welcome! After extensive googling, I found that useful nugget in this forum discussion :
https://stackoverflow.com/questions/54497661/formatting-output-to-show-in-the-messages-section-in-error-list