27

I am guessing from # that it is only a compile-time utility. How can it be used in C/C++ programs?

Did not find much about it on the internet. Any links would be helpful.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Moeb
  • 10,527
  • 31
  • 84
  • 110

3 Answers3

26

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.

It can also be useful if you want to be sure that some block of code is never compiled (for whatever reason).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    what do you mean by "only certain combinations of macros are defined". Can you quote an example? thanks. – Moeb May 08 '10 at 21:11
  • 1
    @cambr: For example, if you have two macros, `DO_X` and `DO_Y` and you want to ensure only one is set at a time, you can wrap an `#error` directive in an `#if (defined DO_X) && (defined DO_Y)` block. – James McNellis May 08 '10 at 21:14
  • 7
    @cambr: Say your company sells Slicer™ and Dicer™. Both products use `blade.c`, but they pass different macros through the build system for slightly different functionality. Then you might use `#if SLICING … #elif DICING … #else #error No functionality selected by makefile.` – Potatoswatter May 08 '10 at 21:35
12

Useful to check compiler settings as well as verifying macro value combinations. Some random examples:

#if !defined(_DLL)
#  error This code will only work properly when compiled with /MD
#endif

#if _WIN32_WINNT < 0x502
#  error Sorry, Windows versions prior to XP SP2 are not supported
#endif

#if defined(_APPLE) && defined(_LINUX)
#  error Conflicting operating system option selected, choose one.
#endif
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Do you really handle the last one in practice? Isn't it the user's responsibility to not be braindead and not use nonsensical compiler flags? You can't check *everything*... – Thomas Nov 26 '13 at 07:48
  • 3
    It is just an example. You tend to add this stuff when you've got one support call too many. Boost is rife with it for example. – Hans Passant Nov 26 '13 at 11:02
4

Here is a link to the documentation of the Gnu preprocessor explaining the #error and #warning directives: http://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html

In particular:

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.

See also this question about the portability of these directives.

Community
  • 1
  • 1
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • 6
    The `#error` directive is part of the C and C++ language standards and is wholly portable. `#warning` is a gcc-specific language extension. – James McNellis May 08 '10 at 21:18