The error message that this directive emits includes the  token-string  parameter. The  token-string  parameter is not subject to macro expansion. This directive is most useful during preprocessing, to notify the developer of a program inconsistency, or the violation of a constraint. The following example demonstrates error processing during preprocessing:
Shows the given error message and renders the program ill-formed  , or shows the given warning message without affecting the validity of the program   (since C++23).
Before its standardization in C++23,    #warning    has been provided by many compilers in all modes as a conforming extension.
It''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''s a preprocessor directive that is used (for example) when you expect one of several possible  -D  symbols to be defined, but none is.
When the preprocessor hits the  #error  directive, it will report the string as an error message and halt compilation; what exactly the error message looks like depends on the compiler.
It causes the compiler (or preprocessor) to output the error message.  In C++, it also renders the translation unit ill-formed (i.e., it causes compilation to fail).
If you have several macros that could be defined and you want to be sure that only certain combinations of them are defined, you can use  #error  to cause compilation to fail if an invalid combination is defined.
One of the least used but potentially most useful features of the C preprocessor is the ANSI-specified #error directive. Here''''''''''''''''''''''''''''''''s a look at a couple of clever uses for #error that have proven invaluable in embedded software development.
The  writer supplied error message  can consist of any printable text. You don''''''''''''''''''''''''''''''''t even have to enclose the text in quotes. (Technically, the message is optional--though it rarely makes sense to omit it.)
The directive ‘ #error ’ causes the preprocessor to report a fatal error.  The tokens forming the rest of the line following ‘ #error ’ are used as the error message.
You would use ‘ #error ’ inside of a conditional that detects a combination of parameters which you know the program does not properly support.  For example, if you know that the program will not run properly on a VAX, you might write
It is used for various situations like terminating compilation process if there are conflicting requests and settings. It is widely used in the build process of various software.
In this C code example, we will raise an error is the value of a macro named power is less than 2. We need to check for two cases:
In the C Programming Language, the #error directive causes preprocessing to stop at the location where the directive is encountered.  Information following the #error directive is output as a message prior to stopping preprocessing.
In this example, we are using the int data type to hold the age of TechOnTheNet in milliseconds.  The int data type has a maximum value of INT_MAX which is defined in the limits.h header file and holds a value of 2^31 - 1 on both 32 and 64 bit systems. (See  here  for other variable types and their sizes.)
Elsewhere ( here  on SO, for example) and even my own memory tells me that  #error  will cause compilation to terminate, but if I compile this:
This is for g++ versions 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4 20160609) and 7.0.0 (built myself 20161128). (There is a similar question asked  here  about MS Visual Studio 2015.)