Macro to cause build failure if parameter not defined?

S

skillzero

Is there a way to cause a compile/preprocessor error in a macro if a
preprocessor symbol is not defined? I want to do the equivalent of
this in a macro:

#if( !defined( SOME_FLAG ) )
#error not defined
#endif

What I want is a way to test if a feature flag is defined to 1 such
that if it's not defined at all (as opposed to defined to 0), I'll get
a compile error. This is to catch cases where people test for a
feature flag without including the right header file, using the right
compile flags, or they just typed it wrong. I want to use it like
this:

#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
not suported. If not defined at all, I want to generate a compile
error.

I know some compilers have options to warn about #if used with
undefined symbols, but I wanted something more portable.
 
V

viza

Hi

Is there a way to cause a compile/preprocessor error in a macro if a
preprocessor symbol is not defined? I want to do the equivalent of this
in a macro:

#if( !defined( SOME_FLAG ) )
#error not defined
#endif

What's wrong with:

#ifndef SOME_FLAG
#error not defined
#endif

Not all preprocessors have #error, but it should be an error in any case.

viza
 
S

skillzero

What's wrong with:

#ifndef SOME_FLAG
#error not defined
#endif

That would require a lot of extra code to test for features (#error if
not defined then test for the feature). I'm trying to automate it so
you just use the macro and it does it all in one shot.
 
K

Keith Thompson

That would require a lot of extra code to test for features (#error if
not defined then test for the feature). I'm trying to automate it so
you just use the macro and it does it all in one shot.

The expansion of a macro cannot contain preprocessor directives.

But it's not all that much extra code. It's 3 lines per feature
rather than 1. I'm not sure what you mean by "#error if not defined
then test for the feature"; are you making this more complicated than
it needs to be?

You can even test multiple features in one line, at the expense of not
being able to have separate error messages:

#if ! ( defined FLAG1 && ! defined FLAG2 && ! defined FLAG3 )
#error "Something is missing"
#endif
 
P

Peter Nilsson

...What I want is a way to test if a feature flag is defined to
1 such that if it's not defined at all (as opposed to defined
to 0), I'll get a compile error. This is to catch cases where
people test for a feature flag without including the right
header file, using the right compile flags, or they just typed
it wrong. I want to use it like this:

#if( HAS_FEATURE( FEATURE_X ) )
... do something if FEATURE_X is defined
#endif

I'll #define FEATURE_X to 1 if supported or #define
FEATURE_X to 0 if not suported. If not defined at all, I
want to generate a compile error.

Nothing is guaranteed to produce an error (not even #error
in C90,) but you can try (untested)...

#define CAT(a,b) a ## b
#define CAT2(a,b) CAT(a,b)
#define HAS_FEATURE_CHECK_0 1
#define HAS_FEATURE_CHECK_1 1
#define HAS_FEATURE(X) (X / CAT2(HAS_FEATURE_CHECK_,X))

Will also fail (or at least diagnose division by zero) if the
feature is not defined to be 0 or 1.
 
P

Peter Nilsson

Eric said:
I think

#define HAS_FEATURE(macro) ( (macro) / defined(macro) )

... might suit you, if you don't mind an uninformative or maybe
even misleading diagnostic.

That runs afoul of 6.10.1p4 [n1256]: "If the token defined is
generated as
a result of [the] replacement process ... the behavior is undefined."
 
S

skillzero

Nothing is guaranteed to produce an error (not even #error
in C90,) but you can try (untested)...

  #define CAT(a,b) a ## b
  #define CAT2(a,b) CAT(a,b)
  #define HAS_FEATURE_CHECK_0 1
  #define HAS_FEATURE_CHECK_1 1
  #define HAS_FEATURE(X) (X / CAT2(HAS_FEATURE_CHECK_,X))

Will also fail (or at least diagnose division by zero) if the
feature is not defined to be 0 or 1.

That's exactly the kind of trick I was looking for. Worked great for
me. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top