Multiple definition of namespace::variable even using ifndef
问题 I know I must be doing something wrong here. rank.h #ifndef RANK_H #define RANK_H namespace mmi { int chunk; void rank(int my_rank); } #endif rank.cpp #include "rank.h" namespace mmi { //do something with chunk } main.cpp #include "rank.h" int main() { mmi::chunk = 1; } And the output of compilation; g++ -g -Wall -std=gnu++11 -c -o main.o main.cpp g++ -g -Wall -std=gnu++11 -c -o rank.o rank.cpp mpic++ main.o rank.o -o main rank.o:(.bss+0x0): multiple definition of `mmi::chunk' main.o:(.bss