Error: redefinition of “a static variable” in C header files
Error: redefinition of “a static variable” in C header files
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.
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,
.c
There is only a tentative definition for
var1
.– Antti Haapala
46 mins ago
var1
use
extern
in the header file: When to use extern in C++– Thomas
45 mins ago
extern
3 Answers
3
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.
var1
var2
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.
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
14 mins ago
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.
file1.h
file2.h
test.cpp
var2
To avoid this declare var2
as a extern
in header .h
file and define in respective source .cpp
file.
var2
extern
.h
.cpp
While in case of var1
it's just declaration
not definition
so it won't throws the error i.e multiple declaration is possible.
var1
declaration
definition
Also the static
declaration like
static
static bool var1;
static bool var1;
means that the variabe var1
is visible only in the current compilation unit.
var1
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.
static
static
static
defined
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.
extern
extern
extern
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
7 mins ago
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.
var2
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Header files are copied into
.c
files. If you include both headers into the same file, you will have multiple definitions.– DeiDei
49 mins ago