Macro to cause build failure if parameter not defined?

Discussion in 'C Programming' started by skillzero@gmail.com, Jun 25, 2008.

  1. Guest

    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.
     
    , Jun 25, 2008
    #1
    1. Advertising

  2. viza Guest

    Hi

    On Wed, 25 Jun 2008 14:54:18 -0700, wrote:

    > 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
     
    viza, Jun 25, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 25, 3:17 pm, viza <> wrote:

    > 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.
     
    , Jun 25, 2008
    #3
  4. "" <> writes:
    > On Jun 25, 3:17 pm, viza <> wrote:
    > > 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.


    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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 26, 2008
    #4
  5. wrote:
    > ...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.

    --
    Peter
     
    Peter Nilsson, Jun 26, 2008
    #5
  6. On 25 Jun, 23:17, viza <> wrote:

    > Not all preprocessors have #error [...]


    all ISO-C compliant one do though

    --
    Nick Keighley
     
    Nick Keighley, Jun 26, 2008
    #6
  7. Eric Sosman wrote:
    > wrote:
    > > #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 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."

    --
    Peter
     
    Peter Nilsson, Jun 26, 2008
    #7
  8. Guest

    On Jun 25, 8:16 pm, Peter Nilsson <> wrote:

    > 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!
     
    , Jun 27, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    625
    Ilias Lazaridis
    Feb 1, 2005
  2. Rob Mandeville
    Replies:
    2
    Views:
    5,638
    Chris \( Val \)
    Jan 25, 2005
  3. Oodini
    Replies:
    1
    Views:
    1,837
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    0
    Views:
    515
  5. Roedy Green
    Replies:
    10
    Views:
    447
    ClassCastException
    Jul 19, 2010
Loading...

Share This Page