This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Declaration of global variables in one header file

Hello everyone,

I'm trying to split the C source code of one file into nearly 10 different. It does not work properly due to global variables declared in one header file but used in more than one C files. Compiling of such a project works fine but the linking errors L104 and L118 occur.

My question: How can I prevent this errors without creating one header file for every C file?

My idea is the following, but it is complicated and additionally unsure if it works:

#ifdef CPVAR
extern unsigned char global_var1;
extern unsigned int global_var2;
extern unsigned long global_var3;
#elseif
unsigned char global_var1;
unsigned int global_var2;
unsigned long global_var3;
#define CPVAR
#endif

Maybe one of the experts knows a less complicated opportunity to prevent (these) linker errors.

Parents
  • Tobias,

    In short, yes... when you use "extern" that's considered a declaration.

    While many will decry what you're trying to do with global variables to be heresy, there is a fairly easy way to do it. Make a file called something obvious like GLOBALS.H so that everyone who has to maintain this code will know of your treason. In it, do something like this:

    #ifdef DEFINEGLOBALSHERE
    #define GLOBAL
    #else
    #define GLOBAL extern
    #endif
    
    GLOBAL unsigned char global_var1;
    GLOBAL unsigned int global_var2;
    GLOBAL unsigned long global_var3;
    

    Then you need to pick a file in which the variables will actually be defined. Main.c is probably a good choice. To do this, on the first line of main.c (before you #include your global.h file), put:

    #define DEFINEGLOBALSHERE

    What this will do is automatically replace the tag GLOBAL before all these variables with "extern" for every file in which the DEFINEGLOBALSHERE tag isn't defined. For the one in which is is defined, it will just delete the GLOBAL tag.

    Note that this only works with plain vanilla definitions. Initializations won't work. Using the _at_ keyword won't work. So, you make some tradeoffs for this simplicity. Good luck and I hope that helps.

Reply
  • Tobias,

    In short, yes... when you use "extern" that's considered a declaration.

    While many will decry what you're trying to do with global variables to be heresy, there is a fairly easy way to do it. Make a file called something obvious like GLOBALS.H so that everyone who has to maintain this code will know of your treason. In it, do something like this:

    #ifdef DEFINEGLOBALSHERE
    #define GLOBAL
    #else
    #define GLOBAL extern
    #endif
    
    GLOBAL unsigned char global_var1;
    GLOBAL unsigned int global_var2;
    GLOBAL unsigned long global_var3;
    

    Then you need to pick a file in which the variables will actually be defined. Main.c is probably a good choice. To do this, on the first line of main.c (before you #include your global.h file), put:

    #define DEFINEGLOBALSHERE

    What this will do is automatically replace the tag GLOBAL before all these variables with "extern" for every file in which the DEFINEGLOBALSHERE tag isn't defined. For the one in which is is defined, it will just delete the GLOBAL tag.

    Note that this only works with plain vanilla definitions. Initializations won't work. Using the _at_ keyword won't work. So, you make some tradeoffs for this simplicity. Good luck and I hope that helps.

Children