多个定义的符号 C++ 错误

2022-01-11 00:00:00 symbols linker c++

我认为 ifndef something #define something body #endif 解决了这个错误,所以我不确定为什么会这样.

I thought ifndef something #define something body #endif solved this error, so I'm not sure why this is happening.

//Library.h
#ifndef __LIBRARY__
#define __LIBRARY__

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>

//file includes
#include "Globals.h"

using namespace std;

#endif //__LIBRARY__

--

//globals.h
//global variables
#ifndef __GLOBAL__
#define __GLOBAL__

#include <vector>
#include <iostream>
#include <string>

//prototypes
bool Poglathon(std::vector<std::string>& text);
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);

//player stats
std::string name;
double str;     //strength
double wis;     //wisdom
double ref;     //reflex
double hp;      //health points
double i;       //initiative
double inte;    //intelligence
double c;       //courage
int gold;       //gold
int xp;         //experience
int ap;         //armour points
int wd;         //weapon damage
int lvl;        //level
int sp;         //skill points

#endif //__GLOBAL__

还有另外两个包含Library.h"的 cpp 文件.

Then there's two other cpp files that include "Library.h".

推荐答案

问题在于,在你的 globals.h 头文件中,你声明了一组默认情况下具有外部链接的变量:即所有全局变量!

The problem is that in your globals.h header file, you're declaring a suite of variables that by default have external linkage: namely, all the globals!

当您在头文件中创建函数原型时,您是在声明一个函数,而不是定义它.对同一个函数进行多次声明是完全合法的,这就是为什么如果几个不同的文件都 #include 相同的标头并声明相同的函数是完全可以的.另一方面,如果您在头文件中有全局变量,那么您就是在定义这些变量.变量在 C++ 中只能定义一次(这称为单一定义规则),如果多个文件定义相同的变量或函数,则会导致链接器错误,因为链接器不知道哪个要使用的版本.顺便说一句,这就是您不 #include .cpp 文件的原因,因为如果您这样做了,您将乘以定义该标头导出的所有函数.

When you prototype a function in a header file, you are declaring a function, but not defining it. It's perfectly legal to have multiple declarations of the same function, which is why if several different files all #include the same header and declare the same function it's perfectly fine. On the other hand, if you have global variables in a header file, you are defining those variables. Variables can only be defined once in C++ (this is called the one-definition rule), and if multiple files define the same variable or function it will cause a linker error because the linker won't know which version to use. This is the reason, by the way, that you don't #include .cpp files, since if you did you'd multiply define all the functions exported by that header.

要解决此问题,您需要在标题中使用 extern 关键字将这些变量 definitions 更改为变量 declarations:

To fix this problem, in the header you'll want to change those variable definitions to variable declarations by using the extern keyword:

//player stats
extern std::string name;
extern double str;     //strength
extern double wis;     //wisdom
extern double ref;     //reflex
extern double hp;      //health points
extern double i;       //initiative
extern double inte;    //intelligence
extern double c;       //courage
extern int gold;       //gold
extern int xp;         //experience
extern int ap;         //armour points
extern int wd;         //weapon damage
extern int lvl;        //level
extern int sp;         //skill points

这将允许任意数量的文件#include这个头文件,因为它们都没有真正定义变量;他们只是声明变量将存在于某处.然后,您应该创建一个新的 .cpp 文件,可能是 globals.cpp,它实际上定义了变量:

This will allow any number of files to #include this header, since none of them are actually defining the variables; they're just declaring that the variables will exist somewhere. Then, you should create a new .cpp file, probably globals.cpp, that actually defines the variables:

#include "globals.h"

std::string name;
double str;     //strength
double wis;     //wisdom
double ref;     //reflex
double hp;      //health points
double i;       //initiative
double inte;    //intelligence
double c;       //courage
int gold;       //gold
int xp;         //experience
int ap;         //armour points
int wd;         //weapon damage
int lvl;        //level
int sp;         //skill points

这些是变量的实际定义,由于它们只存在于一个位置 (globals.cpp),因此您不会再收到任何链接器错误.

These are the actual definitions for the variables, and since they exist in just one place (globals.cpp) you won't get any more linker errors.

希望这会有所帮助!

相关文章