[Prev][Next][Index][Thread]

Re: use of #ifdefs



I have a suggestion about the use of #ifdefs if they
are needed - try to put all the operating system specific switches in a single config header file. 

In the source file itself identify the features
that are operating system specific and give them 
their own switch variable. The configuration file
can determine how the switch variable is configured
for each operating system.

The advantage of this approach is that you don't
get an awful nest of #ifdef's in the source files
and you get to see all the operating system specific
configuration in a single file.


for example, DON'T do this:

/* source.c */
#ifndef __OS2__
pid = getpid();
#endif

On the otherhand, do DO this:

/* config.h */

#define HAVE_GETPID 1
#ifdef __OS2__ 
#undef HAVE_GETPID
#endif

/* source.c */
#include "config.h"

#ifdef HAVE_GETPID
pid = getpid();
#endif

A simple suggestion, but you'd be amazed at how much
UNIX code out there has hacks throughout the source to
deal with platform differences.

jon seymour.



Follow-Ups: