Compiler Diagnostic Messages

Last modified by Microchip on 2024/01/22 16:46

Compiler error and warning messages are produced to help you debug source code. You should attempt to understand what they mean, how to control them, and most importantly: never ignore them.

When building projects, compilers continue with the compilation process for as long as possible, even if they find errors in the source code. A compiler will only terminate if a fatal error is encountered or the number of errors reported becomes excessive. However, this means that one error can sometimes lead to further errors or warnings as the compiler tries to internally process bad or incomplete information. If the compiler issues many diagnostic messages, always begin working through them from the message reported first. In many cases, fixing one error will fix several others that follow.

Although it can be more difficult for a human to read, source statements in the C language can be spread over multiple lines of the source file. This is often done if the statement is very long. This can mean that errors in statements might not be detected until the compiler parser begins checking the subsequent lines of the source file. If you cannot find anything wrong with the line of code reported in a compiler message, check the line or lines of code above.

The most common example of a message that references a falsely reported line number is when there is a missing semicolon from a statement, which is typically reported as being a problem with the statement following the error. Another common mistake that can lead to errors reported in subsequent source code is an incorrectly terminated comment. Be especially mindful that a reported error might be caused by a mistake hidden in a header file included earlier in the code.

If you would like to perform your own compile-time tests, you can issue warnings by using the MPLAB® XC #warning directive. (The MPLAB XC compilers also implement a #error directive.) For example:

1
2
3
4
5
#include <limits.h>

#if (INT_MAX < 8388607)
#warning The int type is not large enough to handle worst-case values
#endif

Although your project might successfully build, do not ignore any warnings issued by the compiler. Warnings can indicate source code that will fail when it is executed. Compiler options are available to disable specific warnings if you can confirm these warnings are irrelevant but remember that warnings that are hidden are often forgotten. Instead, if a warning cannot be removed, consider justifying and documenting why it can be ignored in that circumstance.

If you are trying to debug code that is not executing correctly, try enabling all possible warning messages, in case there is an error you have missed in your source code.

Each MPLAB XC Compiler User’s Guide has an appendix that lists all the error and warning messages that can be produced, along with a description of the cause.