On Thu, Aug 11, 2005 at 10:48:37AM +0100, Richard Kettlewell wrote:
Chris Green chris@areti.co.uk writes:
The problem is how to 'hand them around' between different parts of the code, if they're all globals it's easy to initialise them as well as create them in a file called, say, astime_config.c, this avoids the need to create them and *then* initialise them separately. However, apart from so many globals creating problems of its own, it then requires a 'extern' declaration for every value in a header file so that other source files can access the values.
It's relatively easy to avoid mentioning every global twice. For example:
/* globals.h --------------------------------------------------------------- */
#if DEFINE_GLOBALS # define EXTERN # define INIT(x) =x #else # define EXTERN extern # define INIT(x) #endif
EXTERN int foo INIT(3); EXTERN int bar INIT(99);
/* globals.c --------------------------------------------------------------- */
#define DEFINE_GLOBALS 1 #include "globals.h"
/* any other source file --------------------------------------------------- */
#include "globals.h"
Thanks, yes, I've done similar tricks with #define when wanting a set of enum constants to match a set of string variables. The above certainly does address the globals issue. However it does mean that you have values (other than constants) in the .h file which is (sort of) a bit naughty.
You could of course make all the actual definitions one huge #define and use it in two places with your two sets of EXTERN and INIT defines set differently. This is essentially what I did for my enum/string one.
Thanks for the ideas, I'll probably go down this route or something like it.