g'day,

Matt Graham wrote:
...
> you have to declare globals in 2 places usually.
> 
> once in a single C file in your project. if you put it in
> multiple .C files you'll get a compile error that the
> variable already exists. it's this declaration that actually
> allocates the memory. do it something like:
> 
> int gUserID;
> 
> secondly, in the header file that will be included in
> multiple .C files....
> 
> extern int gUserID;


I used to maintain two .h files, one called "globals.h" and one called
"externs.h", using the declares shown above. The first got included only
in the program's main module (for PalmOS programming, I used the one
with the PilotMain function in it). This works, but you sometimes run
into problems keeping the two files synchronized as a project grows, so
I now declare a single include file, and use a preprocessor define to
sort out when it actually needs to be declared. Here's a simplified
example:

In the header file:


// ################################################
//
//                   globals.h
//
//  Global variables for Nighthawk project.
//
// ################################################
#include <PalmOS.h>

#include "defines.h"    // system-wide defines
#include "functions.h"  // function prototypes

#ifdef MAINMODULE
#define MYPREAMBLE
#else
#define MYPREAMBLE extern
#endif


// Put all the uninitialized global variables here
// MYPREAMBLE will automatically add "extern" as needed...

MYPREAMBLE char fred[128];
...

To use this, in the main module, you use:

#define MAINMODULE
#include "globals.h"



and in every other c file, you use only:

#include "globals.h"

Each time you add a new global, you add a single line in globals.h:

MYPREAMBLE int foo;

and the preprocessor sorts it all out...




Now, I actually add a touch more detail in the if statments to allow me
to include initialized globals, as well. Thus, it might look like this:


#ifdef MAINMODULE
#define MYPREAMBLE
// put initialized variables here
Char gAppName[] = "Nighthawk";
#else
#define MYPREAMBLE extern
// and their externs here
extern char gAppName[];
#endif
...

Of course, you can avoid this complexity if you have an init routine at
the start of your program that sets up things properly, but somethmes
this is not worth the hassle...


                                        - peterd

-- 

---------------------------------------------------------------------
    Peter Deutsch                       [EMAIL PROTECTED]
                      Gydig Software

 "We were told to be very careful - not to buy our train ticket 
  in Princeton, for example because Princeton was a very small 
  station, and if everybody bought train tickets to Albuquerque,
  New Mexico, in Princeton, there would be some suspicions that
  something was up. And so everybody bought their tickets somewhere
  else, except me, because I figured if everybody bought their
  tickets somewhere else...

  So I went to the train station and said, `I want to go to
  Albuquerque, New Mexico,' the man says, `Oh, so all this stuff
  is for *you*!'

  We had been shipping out crates full of counters for weeks and
  expecting that they didn't notice the address was Albuquerque..."

                         - Richard Feyneman,
                           "Surely You're Joking, Mr. Feynman!"

------------------------------------------------------------------

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to