重复的多重定义错误在多个cpps中包含相同的标头 [英] Repeated Multiple Definition Errors from including same header in multiple cpps

查看:157
本文介绍了重复的多重定义错误在多个cpps中包含相同的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,无论我似乎做了什么,我似乎不能避免让Dev C ++吐出大量的多重定义错误,因为我包括相同的头文件在同一个项目中的多个源代码文件。我强烈希望避免将所有源代码转储到一个文件中,并且只包含头一次,因为这将使我的文件很长,难以管理。



基本上,这是发生了什么:

  #ifndef _myheader_h 
#define _myheader_h

typedef struct MYSTRUCT {
int blah;
int blah2; } MYSTRUCT;

MYSTRUCT Job_Grunt;
MYSTRUCT * Grunt =& Job_Grunt;
MYSTRUCT Job_Uruk;
MYSTRUCT * Uruk =& Job_Grunt;

int Other_data [100];

void load_jobs();

#endif

示例Cpp文件this):

  #includemyheader.h

void load_jobs(){

Grunt-> blah = 1;
Grunt-> blah2 = 14;

Uruk-> blah = 2;
Uruk-> blah2 = 15;

return; }

请记住,我有大约5个cpp文件包含这一个标题,每个处理在头文件中找到不同类型的结构。在这个例子中,只有一个结构包含几个成员,当有大约4-6个不同的结构,在实际的头文件中有更多的成员。我已经包含它的所有文件都遵循在这个例子中看到的相同的公式。



现在我明白了标题防护只停止每个单独的cpp文件包括头文件多次。看起来发生的是,当编译器读取每个cpp开始处的include时,它会重新定义头文件,这会导致它抛出以下行和行:



多重定义Uruk,首先定义在这里
多重定义Job_Uruk,首先在这里定义
多重定义Grunt,首先定义在这里
多重定义Job_Grunt,首先定义在这里
多重定义Other_data,首先在这里定义

我会看到一组这样的项目中的每个cpp文件,包括标题。我已经尝试将结构和结构变量的定义移动到cpp文件,但然后其他cpp文件不能看到他们或与他们合作,这是非常重要的,因为我需要项目中的所有文件,以能够工作使用这些结构。



但是关于这个问题的最令人困惑的部分需要更多的解释:



方式我在这个项目中设置这些多个文件是相同的我正在使用的书,所有在一个游戏编程由约翰·港。我在书中为示例项目创建文件时遇到了完全相同的问题,它要求在同一个项目中包含多个cpps的一个头。



我可以键入他们出来,从书中的字,我的意思是字的字...

,我会得到一系列的MD错误的每个cpp项目中。



如果我从书中包含的CD中加载示例项目,它将编译并运行没有问题,虽然文件本身,以及项目选项,



如果我创建了自己的项目文件,只需从CD中添加示例项目的源文件和头文件,这也将编译和运行,虽然我可以找到这些和我的没有区别。



然后,我试图做自己的项目文件,然后创建空白源文件和头文件并将它们添加到它,然后通过从他们想要对应的CD上的文件(相同的工作)复制和粘贴它们的内容来填充它们。
当然,我会得到相同的东西... MD和MD错误信息的行和行。



我绝对不好意思。我重复了所有这些方法多次,并且确定我不是错误的或混杂的代码。似乎只是一些关于预制文件本身的东西;一些配置设置或其他我缺少完全...这将导致他们正确地编译,而我自己的文件不会。

解决方案

由于您在头文件中声明这些变量,并且在每个C ++文件中包含头文件,因此每个C ++文件都有自己的副本。



通常的方法是声明头文件中的任何变量。相反,在单个C ++文件中声明它们,并在其他可能需要它们的其他文件中将它们声明为 extern



我之前处理过的另一种方式,有些人可能认为不愉快...在头文件中声明它们,像这样:

  #ifdef MAINFILE 
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN MYSTRUCT Job_Grunt;
EXTERN MYSTRUCT * Grunt =& Job_Grunt;
EXTERN MYSTRUCT Job_Uruk;
EXTERN MYSTRUCT * Uruk =& Job_Grunt;

然后,在您的C ++文件的一个

  #define MAINFILE 


b $ b

...在 #include 行之前。这将照顾一切,并且(在我个人看来)比必须重新声明每个文件中的所有变量更好。



当然, em> real 解决方案根本不是使用全局变量,但是当你刚开始时很难实现。


So, no matter what I seem to do, I cannot seem to avoid having Dev C++ spew out numerous Multiple Definition errors as a result of me including the same header file in multiple source code files in the same project. I'd strongly prefer to avoid having to dump all my source code into one file and only include the header once, as that's going to make my file very long and difficult to manage.

Essentially, this is what's going on:

#ifndef _myheader_h
#define _myheader_h

typedef struct MYSTRUCT{
int blah;
int blah2; } MYSTRUCT;

MYSTRUCT Job_Grunt;
MYSTRUCT *Grunt = &Job_Grunt;
MYSTRUCT Job_Uruk;
MYSTRUCT *Uruk = &Job_Grunt;

int Other_data[100];

void load_jobs();

#endif

Example Cpp File (They pretty much all look something like this):

#include "myheader.h"

void load_jobs(){

Grunt->blah = 1;
Grunt->blah2 = 14;

Uruk->blah = 2;
Uruk->blah2 = 15;

return; }

Bear in mind that I have about 5 cpp files that include this one header, each one dealing with a different type of struct found in the header file. In this example there was only the one struct containing a couple of members, when there are about 4-6 different structs with many more members in the actual header file. All the files I've included it in follow the same formula as you see in this example here.

Now I understand that the header guard only stops each individual cpp file from including the header file more than once. What would seem to be happening is that when the compiler reads the include at the start of each cpp, it defines the header file all over again, which is causing it to spit out lines and lines of:

Multiple Definition of Uruk, first defined here  
Multiple Definition of Job_Uruk, first defined here  
Multiple Definition of Grunt, first defined here  
Multiple Definition of Job_Grunt, first defined here  
Multiple Definition of Other_data, first defined here

I'll see a set of this for just about every cpp file in the project which includes the header. I've tried moving the definitions of the struct and the struct variables to the cpp files, but then the other cpp files cannot see them or work with them, which is very important as I need all files in the project to be able to work with these structs.

But the single most confusing part about this problem requires a little more explanation:

The way I'm setting up these multiple files in this project is identical to the book I'm working with, All In One Game Programming by John S. Harbour. I ran into the exact same problems when I created the files for example projects in the book which called for one header included by multiple cpps in the same project.

I could type them out, word for word from the book, and I do mean word for word...
and I'd get the series of MD errors for every cpp in the project.

If I loaded the example project from the CD included with the book, it would compile and run without a problem, allthough the files themselves, as well as the project options, were by all appearances identical to the ones I had created.

If I created my own project file, and simply added the source and header files for the example project from the CD, this, too, would also compile and run, though I can find no difference between those and mine.

So then, I tried making my own project file, then creating the blank source and header files and adding them to it, and then filling them by copying and pasting their contents from the files on the CD they were meant to correspond to(the same ones that had worked). And sure enough, I'd get the same thing...lines and lines of MD error messages.

I'm absolutely baffled. I've repeated all these methods multiple times, and am certain I'm not mistyping or miscopying the code. There just seems to be something about the premade files themselves; some configuration setting or something else I'm missing entirely...that will cause them to compile correctly while the files I make myself won't.

解决方案

Since you're declaring those variables in the header file, and including the header file in each C++ file, each C++ file has its own copy of them.

The usual way around this is to not declare any variables within header files. Instead, declare them in a single C++ file, and declare them as extern in all the other files that you might need them in.

Another way I've handled this before, which some people might consider unpleasant... declare them in the header file, like this:

#ifdef MAINFILE
    #define EXTERN
#else
    #define EXTERN extern
#endif

EXTERN MYSTRUCT Job_Grunt;
EXTERN MYSTRUCT *Grunt = &Job_Grunt;
EXTERN MYSTRUCT Job_Uruk;
EXTERN MYSTRUCT *Uruk = &Job_Grunt;

Then, in one of your C++ files, add a...

#define MAINFILE

...before your #include lines. That will take care of everything, and is (in my personal opinion) a lot nicer than having to redeclare all of the variables in every file.

Of course, the real solution is not to use global variables at all, but when you're just starting out that's hard to achieve.

这篇关于重复的多重定义错误在多个cpps中包含相同的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆