我知道预处理器命令是头文件的重要组成部分,以防止多次定义变量和类。
我一直遇到我的变量被多次定义的问题——即使是预处理器包装器也是如此。这是一个遇到编译器错误的示例项目:
标题:
// TestInclude.h
#ifndef TESTINCLUDE_H_
#define TESTINCLUDE_H_
int myInt;
#endif /*TESTINCLUDE_H_*/
C++:
// TestInclude.cpp
#include <iostream>
#include "IncludeMe.h"
#include "TestInclude.h"
int main( int argc, char* args[] )
{
std::cin >> myInt;
IncludeMe thisClass;
std::cin >> myInt;
}
标题:
// IncludeMe.h
#ifndef INCLUDEME_H_
#define INCLUDEME_H_
class IncludeMe
{
private:
int privateInt;
public:
IncludeMe();
};
#endif /*INCLUDEME_H_*/
C++:
// IncludeMe.cpp
#include <iostream>
#include "IncludeMe.h"
#include "TestInclude.h"
IncludeMe::IncludeMe()
{
std::cout << "myInt: " << myInt;
}
然后我像这样编译:
生成文件:
all:
g++ -g -o TestInclude TestInclude.cpp IncludeMe.cpp
我收到以下错误:
/tmp/ccrcNqqO.o:在函数“IncludeMe”中:/ home/quakkels/Projects/c++/
TestInclude/IncludeMe.cpp:6:“myInt”的多个定义
/tmp/ccgo6dVT.o:/home/quakkels/Projects/ c++/TestInclude/TestInclude.cpp:7: 首先定义在这里
collect2: ld 返回1 退出状态
make: *** [all] Error 1
当我在头文件中使用预处理器条件时,为什么会出现此错误?