473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

conditional inclusion

#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?
--
aegis

p.s. google is the one that damaged my formatting
I used spaces not tabs too

Nov 14 '05 #1
5 1598
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"

Nov 14 '05 #2
On Sun, 19 Dec 2004 10:01:32 +0100
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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


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'
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
On 18 Dec 2004 22:58:42 -0800, "aegis" <ae***@mad.scientist.com> wrote
in comp.lang.c:
#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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

In article <60********************************@4ax.com>, Jack Klein <ja*******@spamcop.net> writes:

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.

--
Michael Wojcik mi************@microfocus.com

How can I sing with love in my bosom?
Unclean, immature and unseasonable salmon. -- Basil Bunting
Nov 14 '05 #5

"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:cq*******@news3.newsguy.com...

In article <60********************************@4ax.com>, Jack Klein

<ja*******@spamcop.net> writes:

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.


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
1
by: David Winter | last post by:
(Sorry - couldn't find a generic DocBook NG - I hope this is close enough.) I'm considering moving my documentation and translation business - which is currently done in proprietary formats such...
16
by: Nathan Funk | last post by:
I used to work as a web designer a couple of years ago, but I haven't been closely in touch in the past years. Has anything changed recently for managing content that is common among many pages...
5
by: Dave | last post by:
Hello all, To protect against multiple inclusions, it is standard practice to enclose the contents of a header file in a construct like this: #ifndef FOO_INCLUDED #define FOO_INCLUDED .......
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
1
by: cnakam | last post by:
Greetings I have a simple MS-Access database with 2 tables in a many-to-many relationship. One is a table of diseases and one is a table of symptoms. I need to create a form where all the...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
2
by: Lionel B | last post by:
I frequently seem to run into the following annoyance regarding template class specialisation: I have a template class which implements, for a general template parameter, some basic functionality...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.