XPost: comp.os.linux.misc, alt.folklore.computers   
      
   In alt.folklore.computers Charlie Gibbs wrote:   
   > In multi-module programs I define my globals in a .h file as follows:   
   >   
   > common.h   
   > --------   
   > #ifdef PRIMARY   
   > #define GLOBAL   
   > #else   
   > #define GLOBAL extern   
   > #endif   
   >   
   > foo.h   
   > -----   
   > #include "common.h"   
   > GLOBAL int foo;   
   >   
   > foo1.c   
   > ------   
   > #define PRIMARY   
   > #include "foo.h"   
   > int main(int argc, char **argv)   
   > {   
   > setfoo();   
   > printf("foo is %d\n", foo);   
   > exit(0);   
   > }   
   >   
   > foo2.c   
   > ------   
   > #include "foo.h"   
   > void setfoo()   
   > {   
   > foo = 5;   
   > }   
   >   
   > It works for me; I like having only one declaration of "foo"   
   > in my source modules.   
      
    I used to do that, and I eventually didn't like it. I then switched to   
   declaring all my global variables in one file:   
      
   globals.c   
   --------   
      
   int c_maxitems;   
   char const *c_name = "Blah de blah blah";   
   int g_foo;   
      
    This file will also contain the code to set global variables that I   
   consider "constant" (the variables that start with "c_"). This allows them   
   to be set at program start up. Then the include file:   
      
   globals.h   
   ---------   
      
   extern int const c_maxitems;   
   extern char const *const c_name;   
   extern int g_foo;   
      
   extern int global_init(int,char *[]);   
      
   Note: "globals.c" will never include "gloabls.h". Oh, and the weird const   
   placement? That avoid the that weird C-spiral rule. The way I use "const"   
   these days means it applies to the thing on the right:   
      
    char * p; // mutable pointer to mutable char   
    char const * q; // mutable pointer to constant char   
    char *const r; // constant pointer to mutable char   
    char const *const s; // constant pointer to constant char   
      
    Recently, I've been writing code with no global variables. It's been a   
   fun experiment.   
      
    -spc (Why yes, I do have a structure that gets passed to every function,   
    why do you ask?)   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|