代码保护失败

拿这个文件:

#ifndef A_H #define A_H char EL[] = "el"; #endif 

a.cpp

 #include "ah" 

BH

 #ifndef B_H #define B_H #include "ah" #endif 

b.cpp

 #include "bh" 

main.cpp中

 #include "bh" #include "ah" int main() { } 

这只是一个例子,但我真的有这个问题:

 g++ -c a.cpp g++ -c b.cpp g++ -c main.cpp g++ -o main main.o ao bo ao:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first defined here bo:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first defined here collect2: ld returned 1 exit status 

为什么以及如何解决?

如果您将定义包含在多个翻译单元中,则包含保护不会保护您不会多次定义对象!

作为解决方案,永远不要在标题中定义东西,而只是声明它们:

 // header extern char EL[2]; // TU #include "header.h" char EL[2] = "el"; // Other consumer #include "header.h"; // can now use EL 

(当然也有例外;例如,类定义很好(但类成员函数定义不是(但内联的是)) – 小心。)


我应该添加,或者您可以在头文件中说static ,以使定义对每个TU都是私有的:

 // header static char EL[] = "EL"; // every TU gets a copy 

(但在C ++ 0x中,您不能使用静态链接的对象作为模板参数。)