For example a head file a.hpp
int foo;and this file will be included by two cpp files, then at link time compiler complains multiple definitions of foo; The problem roots in that ``int foo'' stands for both the declaration and definition;
The correct way is using ``extern'' to tell compiler it is just declaration , not a definition, which will be put in the cpp file. For example a.hpp contains
extern int foo;and in one of the cpp file (viewed as an implementation file) put
int foo;Then we just one implementation with multiple declaration;
Adding static qualifier before the declaration seems can have compiler work without any error.
static int foo;but actually it is very dangerous since if two cpp files include the head file they actually have their own copy of integer foo rather than a global foo;
If the value is const, then in the head file
static const int foo = 9; /* or const int foo =9; */It is fine since compiler will recognize it is unnecessary to keep multiple copies of foo for each cpp file;