How do I force a compiler error if two #defines exist?

Discussion in 'C Programming' started by Richard Tobin, Sep 24, 2007.

  1. In article <>,
    Eliot <> wrote:

    >#define firstdef
    >#define seconddef
    >
    >If no defines are present or only one is present all is OK. I would
    >like to add a self check to the code that prevents compilation if both
    >#defines are present. Is this possible?


    #if defined(firstdef) && defined(seconddef)
    #error Mustn't define both
    #endif

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 24, 2007
    #1
    1. Advertising

  2. Richard Tobin

    Eliot Guest

    I have a project which may be compiled with some #defines.

    Say:-
    #define firstdef
    #define seconddef

    If no defines are present or only one is present all is OK. I would
    like to add a self check to the code that prevents compilation if both
    #defines are present. Is this possible?
     
    Eliot, Sep 24, 2007
    #2
    1. Advertising

  3. Eliot <> wrote:
    >I have a project which may be compiled with some #defines.
    >
    >Say:-
    >#define firstdef
    >#define seconddef
    >
    >If no defines are present or only one is present all is OK. I would
    >like to add a self check to the code that prevents compilation if both
    >#defines are present. Is this possible?


    #if defined(fistdef) && defined(secondef)
    #error Don't do this!
    #endif


    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
     
    Roberto Waltman, Sep 24, 2007
    #3
  4. Richard Tobin

    r6144 Guest

    On 9 24 , 9 27 , Eliot <> wrote:
    > I have a project which may be compiled with some #defines.
    >
    > Say:-
    > #define firstdef
    > #define seconddef
    >
    > If no defines are present or only one is present all is OK. I would
    > like to add a self check to the code that prevents compilation if both
    > #defines are present. Is this possible?


    #if defined(firstdef) && defined(seconddef)
    #error "Cannot define both firstdef and seconddef"
    #endif
     
    r6144, Sep 24, 2007
    #4
  5. Richard Tobin

    Richard Guest

    Eliot <> writes:

    > I have a project which may be compiled with some #defines.
    >
    > Say:-
    > #define firstdef
    > #define seconddef
    >
    > If no defines are present or only one is present all is OK. I would
    > like to add a self check to the code that prevents compilation if both
    > #defines are present. Is this possible?


    #ifdef firstdef
    #ifdef seconddef
    garbage(NONEXISTANT);
    ....

    where garbage is a missing function and/or NONEXISTANT is undefined is
    ugly but will do what you want. I cant imagine why you would want to
    force an error though.
     
    Richard, Sep 24, 2007
    #5
  6. In article <>,
    Richard <> wrote:

    >#ifdef firstdef
    >#ifdef seconddef


    >I cant imagine why you would want to force an error though.


    Presumably defining both will result in erroneous code being produced.
    Even if it's something like including two different but incompatible
    headers, which would probably produce a compile-time error anyway,
    it's better to detect errors as soon as possible and produce an error
    message that gets to the root of the problem. For example, "Can't use
    both X-windows and Y-windows" is better than "Redefinition of struct
    window".

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 24, 2007
    #6
  7. Richard Tobin

    Eliot Guest

    Thanks for the fitting and fast responses!
     
    Eliot, Sep 24, 2007
    #7
  8. Roberto Waltman <> writes:
    > Eliot <> wrote:
    >>I have a project which may be compiled with some #defines.
    >>
    >>Say:-
    >>#define firstdef
    >>#define seconddef
    >>
    >>If no defines are present or only one is present all is OK. I would
    >>like to add a self check to the code that prevents compilation if both
    >>#defines are present. Is this possible?

    >
    > #if defined(fistdef) && defined(secondef)
    > #error Don't do this!
    > #endif


    The text following "#error" must be a sequence of preprocessing
    tokens. The unmatched apostrophe violates this requirement. C99
    6.4p3 says:

    The categories of preprocessing tokens are: header names,
    identifiers, preprocessing numbers, character constants, string
    literals, punctuators, and single non-white-space characters
    that do not lexically match the other preprocessing token
    categories. If a ' or a " character matches the last category,
    the behavior is undefined.

    It's likely that the compiler will either complain about the
    apostrophe, or just incorporate it into the error message, but it's
    not guaranteed; conceivably the entire #error directive could be
    quietly ignored, or something *really* bad could happen.

    It's better to use a valid string literal:

    #error "Don't do this!"

    Amusingly, Richard Tobin suggested something very similar:

    #error Mustn't define both

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 24, 2007
    #8
  9. In article <>,
    Keith Thompson <> wrote:

    >The text following "#error" must be a sequence of preprocessing
    >tokens. The unmatched apostrophe violates this requirement.


    I had a vague recollection that something like that might be true, and
    I didn't have the standard handy, so I tried compiling in gcc with all
    the options set, and it didn't complain. Usually that's enough, but
    evidently not in this case.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 24, 2007
    #9
  10. Richard Tobin

    Army1987 Guest

    On Mon, 24 Sep 2007 06:27:41 -0700, Eliot wrote:

    > I have a project which may be compiled with some #defines.
    >
    > Say:-
    > #define firstdef
    > #define seconddef
    >
    > If no defines are present or only one is present all is OK. I would
    > like to add a self check to the code that prevents compilation if both
    > #defines are present. Is this possible?


    #if defined firstdef && defined seconddef
    #error "Both firstdef and seconddef are defined"
    #endif
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
     
    Army1987, Sep 24, 2007
    #10
  11. Richard Tobin

    Flash Gordon Guest

    Richard Tobin wrote, On 24/09/07 21:17:
    > In article <>,
    > Keith Thompson <> wrote:
    >
    >> The text following "#error" must be a sequence of preprocessing
    >> tokens. The unmatched apostrophe violates this requirement.

    >
    > I had a vague recollection that something like that might be true, and
    > I didn't have the standard handy, so I tried compiling in gcc with all
    > the options set, and it didn't complain. Usually that's enough, but
    > evidently not in this case.


    Ah, but it produced a diagnostic I'm sure even if it was what you
    thought you were specifying, so it still met the letter of the standard.
    --
    Flash Gordon
     
    Flash Gordon, Sep 24, 2007
    #11
  12. In article <-gordon.me.uk>,
    Flash Gordon <> wrote:

    >>> The text following "#error" must be a sequence of preprocessing
    >>> tokens. The unmatched apostrophe violates this requirement.


    >> I had a vague recollection that something like that might be true, and
    >> I didn't have the standard handy, so I tried compiling in gcc with all
    >> the options set, and it didn't complain. Usually that's enough, but
    >> evidently not in this case.


    >Ah, but it produced a diagnostic I'm sure even if it was what you
    >thought you were specifying, so it still met the letter of the standard.


    Yes, I suppose it did...

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 24, 2007
    #12
  13. Richard Tobin

    CBFalconer Guest

    Eliot wrote:
    >
    > I have a project which may be compiled with some #defines.
    >
    > Say:-
    > #define firstdef
    > #define seconddef
    >
    > If no defines are present or only one is present all is OK. I
    > would like to add a self check to the code that prevents
    > compilation if both #defines are present. Is this possible?


    #ifdef firstdef
    # ifdef seconddef
    # error "Bad definition set"
    # endif
    #endif

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 25, 2007
    #13
  14. Flash Gordon <> writes:
    > Richard Tobin wrote, On 24/09/07 21:17:
    >> In article <>,
    >> Keith Thompson <> wrote:
    >>
    >>> The text following "#error" must be a sequence of preprocessing
    >>> tokens. The unmatched apostrophe violates this requirement.

    >> I had a vague recollection that something like that might be true,
    >> and
    >> I didn't have the standard handy, so I tried compiling in gcc with all
    >> the options set, and it didn't complain. Usually that's enough, but
    >> evidently not in this case.

    >
    > Ah, but it produced a diagnostic I'm sure even if it was what you
    > thought you were specifying, so it still met the letter of the
    > standard.


    The stray apostrophe invokes undefined behavior, so anything the
    compiler did would meet the letter of the standard.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 25, 2007
    #14
  15. On Sep 25, 2:26 am, Army1987 <> wrote:
    > On Mon, 24 Sep 2007 06:27:41 -0700, Eliot wrote:
    > > I have a project which may be compiled with some #defines.

    >
    > > Say:-
    > > #define firstdef
    > > #define seconddef

    >
    > > If no defines are present or only one is present all is OK. I would
    > > like to add a self check to the code that prevents compilation if both
    > > #defines are present. Is this possible?

    >
    > #if defined firstdef && defined seconddef
    > #error "Both firstdef and seconddef are defined"
    > #endif
    > --
    > Army1987 (Replace "NOSPAM" with "email")
    > A hamburger is better than nothing.
    > Nothing is better than eternal happiness.
    > Therefore, a hamburger is better than eternal happiness.


    yes !!

    Karthik Balaguru
     
    karthikbalaguru, Sep 25, 2007
    #15
    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. Y.S.
    Replies:
    3
    Views:
    1,083
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,148
    Phlip
    Jul 25, 2004
  3. theotyflos
    Replies:
    3
    Views:
    508
    Thomas Matthews
    Feb 19, 2004
  4. frankkirchner
    Replies:
    0
    Views:
    184
    frankkirchner
    Oct 9, 2006
  5. Markus Mohr
    Replies:
    7
    Views:
    275
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page