3

I have some static variables (say, var1 and var2) declared in two different files. The variables have same name in both files. Some variables (say var1) are not initialized in their declaration and some are (var2), like following.

file1.h

static bool var1;
static bool var2 = false;

file2.h

static bool var1;
static bool var2 = false;

According to my understanding, static variables are only restricted to the c files(or h files) they're declared in, so I should be safe having same variable names in multiple header files. But when I compile code, I get error "redefinition of var2", only for the variables that have been initialized.

  1. Why this error occurs only for var2 ?
  2. Is my implementation alright ?

Edit: since some mentioned to use extern keyword, I'd like to clarify that both var1 and var2 are supposed to have different values in different c files, and should only be restricted to their respective files,

Salman
  • 81
  • 9

3 Answers3

1

No. The multiple declaration of var1 is okay, but the multiple definition of var2 is not. You can't initialize a variable twice...even if the values are the same.

I solve problems like this using preprocessor guards, such as:

#if !defined(MY_APP__VARS_DEFINED)
static int var1, var2=0;
#define MY_APP__VARS_DEFINED
#endif

Even then, I don't recommend duplicating the definitions in multiple header files. It's a maintenance problem. Sooner or later, someone is likely to change that initial value in one header and not find all other headers it's defined in. It also makes renaming ("refactoring") harder. (...and violates the DRY Principle.)

You might want to rethink your design, though. Global variables usually lead to brittle applications; harder to maintain and easy to break.

Mike Housky
  • 3,959
  • 1
  • 17
  • 31
  • actually these variables are supposed to hold different values in different c files, but since the information is almost similar I named them same, they're supposed to be global inside one c file, but should not be global to the whole program, isn't that's why static keyword used for ? just like we use same name local variables in multiple functions(e.g., 'i' in for loops), I want same global variables in multiple compilation unit, is it possible ? – Salman Jul 02 '18 at 12:46
  • @Salman Yes, it's possible. Just use the preprocessor guard idea as shown. It's probably a good idea to initialize *all* such variables; giving a recognizable nonsense value (like 0xDEADDEAD or some such) for "non-initialized" variables. I'd still put the declarations in common header file that other headers can include if they need to. Single point of maintenance, DRY, one place to put comments that generate developer docs, etc. – Mike Housky Jul 02 '18 at 13:20
0

Why this error occurs only for var2 ? if you include file1.h and file2.h in same source file test.cpp then var2 will be having two definition and compiler is unable to choose which one to take and throws the error.

To avoid this declare var2 as a extern in header .h file and define in respective source .cpp file.

While in case of var1 it's just declaration not definition so it won't throws the error i.e multiple declaration is possible.

Also the static declaration like

static bool var1;

means that the variabe var1 is visible only in the current compilation unit.

Side note, static variable having internal linkage i.e it hides static variable from other translation units. Though static variables can be defined in multiple translation units.

And extern variable having external linkage, i.e extern variable will be visible to other translation units, but the extern variable should be define only in one translation unit.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • actually these variables are supposed to hold different values in different c files, but since the information is almost similar I named them same, they're supposed to be global inside one c file, but should not be global to the whole program, isn't that's why static keyword used for ? just like we use same name local variables in multiple functions(e.g., 'i' in for loops), I want same global variables in multiple compilation unit, is it possible ? – Salman Jul 02 '18 at 12:53
0

If a source file is including both file1.h and file2.h, it will have multiple definitions of var2. That's what causing the error.

Rather than declaring / defining these static variables in header files, put them directly the necessary C source files. That way you won't have to deal with multiple definitions.

dbush
  • 205,898
  • 23
  • 218
  • 273