On 25/10/2007, Marcel Birthelmer wrote: > James, > the way we do this at work usually is by defining an INIT macro, like so: > > #ifdef FOO_INIT > #define FOO_VAR > #else > #define FOO_VAR extern > #endif > > and then all the variables in foo.h would be declared as > > FOO_VAR int foo1; etc. > > When including foo.h from foo.c, you would define FOO_INIT: > > #define FOO_INIT > #include foo.h > > That way, in foo.c the variables are declared as globals. > > Everywhere else (main.c or whatnot), you would just do #include foo.h, > and there the variables are then marked as extern, referring to the > variable defined in foo.c . > > This gets a little bit tricky when you want to initialize the > variables - you have to use a more complicated macro for that. #ifdef FOO_INIT #define VAR_DECLARE(var, init) extern var #else #define VAR_DECLARE(var, init) var = init #endif [header] VAR_DECLARE(int a, 42); VAR_DECLARE(static int b, 12); VAR_DECLARE(volatile int c, 44); const int d = 45; [/header] The const is special since it is used as a compiler optimization that requires it to be defined there. The rest means that there's an actual variable that needs to be defined once in the program and declared everytime you want to use it. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist