conditional inclusion

A

aegis

#ifdef defined(VERSION1)
#define INCLUDE "version1.h"
#elif defined(VERSION2)
#define INCLUDE "version2.h"
#endif

#include INCLUDE

vs'

#ifdef defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif


which do you prefer and why?
 
E

Emmanuel Delahaye

aegis wrote on 19/12/04 :
#ifdef defined(VERSION1)
#define INCLUDE "version1.h"
#elif defined(VERSION2)
#define INCLUDE "version2.h"
#endif

#include INCLUDE

vs'

#ifdef defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif

which do you prefer and why?

None is working. Try this:

#if defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
F

Flash Gordon

aegis wrote on 19/12/04 :

None is working. Try this:

#if defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif

Or

#ifdef VERSION1
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif

I generally prefer "#ifdef" to "#if defined" because it is less typing
and less to read. However, when you need to test for multiple defines it
is easier to use '#if defined'
 
J

Jack Klein

#ifdef defined(VERSION1)
#define INCLUDE "version1.h"
#elif defined(VERSION2)
#define INCLUDE "version2.h"
#endif

#include INCLUDE

vs'

#ifdef defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#endif


which do you prefer and why?

Neither. What I actually use when I need it, macro names changed from
the actual ones in my source to yours, is:

#undef VERSION_OK

#ifdef VERSION1
#ifdef VERSION2
#error Both VERSION1 and VERSION2 defined
#else
#define VERSION_OK
#endif
#endif

#ifndef VERSION_OK
#error Version Not Specified!
#endif

This catches the error of not defining either version type as early as
possible, before you run into potentially cryptic errors about
undefined symbols later on.

As to the actual #include directives, I would always use the second
form. Source code is for human readers, not just compilers, and if
someone is trying to understand the source, they need to directly see
the names of files included, not have to back up half a page to find
out. Also, I would not be surprised if some non-compiler tools might
not support the macro properly.
 
M

Michael Wojcik

Neither. What I actually use when I need it, macro names changed from
the actual ones in my source to yours, is:

#undef VERSION_OK

#ifdef VERSION1
#ifdef VERSION2
#error Both VERSION1 and VERSION2 defined
#else
#define VERSION_OK
#endif
#endif

#ifndef VERSION_OK
#error Version Not Specified!
#endif

This catches the error of not defining either version type as early as
possible, before you run into potentially cryptic errors about
undefined symbols later on.

I like the robustness of this version, but I find using the defined()
operator better suited to my tastes:

#if defined(VERSION1) && defined(VERSION2)
#error "Both VERSION1 and VERSION2 defined"
#elif defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#else
#error "Version not specified"
#endif

(I've quoted the argument to the #error directives because some
characters can't appear "bare" in one - the argument to #error has to
be a set of pp-tokens, so for example English contractions are illegal,
because they contain an unescaped single-quote character. I find it
easiest to just always use a quoted string with #error - and not use
double-quote characters within it, of course.)

I find that version more readable than Jack's, and it avoids using
the additional VERSION_OK macro, but this is really just a matter
of personal style.
 
C

Carlo

Michael Wojcik said:
I like the robustness of this version, but I find using the defined()
operator better suited to my tastes:

#if defined(VERSION1) && defined(VERSION2)
#error "Both VERSION1 and VERSION2 defined"
#elif defined(VERSION1)
#include "version1.h"
#elif defined(VERSION2)
#include "version2.h"
#else
#error "Version not specified"
#endif

(I've quoted the argument to the #error directives because some
characters can't appear "bare" in one - the argument to #error has to
be a set of pp-tokens, so for example English contractions are illegal,
because they contain an unescaped single-quote character. I find it
easiest to just always use a quoted string with #error - and not use
double-quote characters within it, of course.)

I find that version more readable than Jack's, and it avoids using
the additional VERSION_OK macro, but this is really just a matter
of personal style.

Very informative. Although the word 'or' is not mentioned, this method
highlights the difference between inclusive and exclusive 'or'. Carlo
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top