Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do I force a compiler error if two #defines exist?

Reply
Thread Tools

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

 
 
Richard Tobin
Guest
Posts: n/a
 
      09-24-2007
In article <. com>,
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.
 
Reply With Quote
 
 
 
 
Eliot
Guest
Posts: n/a
 
      09-24-2007
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?

 
Reply With Quote
 
 
 
 
Roberto Waltman
Guest
Posts: n/a
 
      09-24-2007
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 ]
 
Reply With Quote
 
r6144
Guest
Posts: n/a
 
      09-24-2007
On 9 24 , 9 27 , Eliot <e...@hotmail.com> 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

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-24-2007
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.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-24-2007
In article <rgrks4->,
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.
 
Reply With Quote
 
Eliot
Guest
Posts: n/a
 
      09-24-2007
Thanks for the fitting and fast responses!

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-24-2007
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) kst- <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"
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-24-2007
In article <>,
Keith Thompson <kst-> 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.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-24-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Nike air force one, air force 1, air force one low cut, air force one salewholeta@163.com Digital Photography 3 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57