代码保护失败

Rug*_*rra 1 c c++ include-guards linker-errors multiple-definition-error

拿这个文件:

#ifndef A_H
#define A_H

char EL[] = "el";
#endif
Run Code Online (Sandbox Code Playgroud)

a.cpp

#include "a.h"
Run Code Online (Sandbox Code Playgroud)

BH

#ifndef B_H
#define B_H

#include "a.h"

#endif
Run Code Online (Sandbox Code Playgroud)

b.cpp

#include "b.h"
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "b.h"
#include "a.h"

int main() { }
Run Code Online (Sandbox Code Playgroud)

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

g++ -c a.cpp
g++ -c b.cpp
g++ -c main.cpp
g++ -o main main.o a.o b.o


a.o:(.data+0x0): multiple definition of `EL'
main.o:(.data+0x0): first defined here
b.o:(.data+0x0): multiple definition of `EL'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

为什么以及如何解决?

Ker*_* SB 8

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

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

// header
extern char EL[2];

// TU
#include "header.h"
char EL[2] = "el";

// Other consumer
#include "header.h";
// can now use EL
Run Code Online (Sandbox Code Playgroud)

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


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

// header
static char EL[] = "EL";  // every TU gets a copy
Run Code Online (Sandbox Code Playgroud)

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