Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Undefined Macro

In C and C++, when using a macro like so:

#if ( 1 == __MY_MACRO__ )
// Some code
#endif

The compiler will not catch if MY_MACRO is not defined and will consider it 0. This could cause a lot of hidden bugs when the design of the code is intended such that the macro must be defined (non-zero).

Is there away to get to compiler to report this, even if the compiler natively doesn't look for such thing?

like image 924
phandinhlan Avatar asked Sep 06 '26 08:09

phandinhlan


2 Answers

Use #if defined(__MY_MACRO__) to test if the macro value is defined.

like image 58
AAT Avatar answered Sep 07 '26 21:09

AAT


#ifndef __MY_MACRO__
  #error "MY_MACRO NOT DEFINED"
#endif
like image 23
BLUEPIXY Avatar answered Sep 07 '26 21:09

BLUEPIXY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!