I just build a simple C++ project. The codes are shown in the follows:
-------- head.h --------
#ifndef _HEAD_H_
#define _HEAD_H_
int my_var = 100;
#en
You are defining my_var
once per compilation unit. Remember that include guards operate on a per compilation unit basis.
To remedy, you should declare my_var
as extern
in the header:
#ifndef _HEAD_H_
#define _HEAD_H_
extern int my_var;
#endif
and define it in one of the source files using
int my_var = 100;
Then the linker sees only one definition and all will be fine.