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

R

Richard Tobin

Eliot said:
#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
 
E

Eliot

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?
 
R

Roberto Waltman

Eliot said:
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 ]
 
R

r6144

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
 
R

Richard

Eliot said:
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.
 
R

Richard Tobin

Richard said:
#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
 
K

Keith Thompson

Roberto Waltman said:
#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
 
R

Richard Tobin

Keith Thompson said:
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
 
A

Army1987

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
 
F

Flash Gordon

Richard Tobin wrote, On 24/09/07 21:17:
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.
 
R

Richard Tobin

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.
[/QUOTE]
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
 
C

CBFalconer

Eliot said:
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
 
K

Keith Thompson

Flash Gordon said:
Richard Tobin wrote, On 24/09/07 21:17:

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.
 
K

karthikbalaguru

#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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top