“多重定义”C++编译器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/685439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Multiple definition of" C++ compiler error
提问by Chad
I can't seem to get rid of these seemingly random compiles errors in one of my classes. I get about 4 errors such as:
我似乎无法摆脱我的一个类中这些看似随机的编译错误。我收到大约 4 个错误,例如:
multiple definition of `draw_line(float, float, float, float)'
and
和
multiple definition of `near_far_clip(float, float, float*, float*, float*, float*, float*, float*)'
that are flagged in the middle of the method.
在方法中间标记的那些。
I also consistently get multiple definition of `stack'
in the middle of another method. stack
is a global variable in a totally different file. It isn't even mentioned in the file I'm getting the error in.
我也始终multiple definition of `stack'
处于另一种方法的中间。stack
是一个完全不同的文件中的全局变量。在我收到错误的文件中甚至没有提到它。
I tried separating the error prone file into .h and .cpp files (was originally just a .cpp) and nothing about the error changed...
我尝试将容易出错的文件分成 .h 和 .cpp 文件(最初只是一个 .cpp),但错误没有任何改变......
I don't have duplicate methods. I only have one #include
of lines.h and there is an #ifndef
clause at the beginning. All these errors appear in the .cpp file.
我没有重复的方法。我只有一个#include
lines.h,#ifndef
开头有一个子句。所有这些错误都出现在 .cpp 文件中。
Any ideas what it could be?
任何想法可能是什么?
Alright, I got the code up:
好的,我得到了代码:
The lines.cpp is a converted .c file I received from my instructor. I included the makefile just in case, because I always have problems with it. I also annotated exactly where the errors were flagged in the file, but they seem pretty random so I don't know if it's particularly important. I abandoned the .h file because it wasn't solving anything or helping. I believe it will be easier to find the mistake without it.
lines.cpp 是我从我的导师那里收到的转换后的 .c 文件。我包含了 makefile 以防万一,因为我总是遇到问题。我还准确地注释了文件中标记错误的位置,但它们看起来很随机,所以我不知道它是否特别重要。我放弃了 .h 文件,因为它没有解决任何问题或帮助。我相信没有它会更容易找到错误。
Here is the requested main.cpp file(there is no .h).
这是请求的main.cpp 文件(没有 .h)。
I remade the lines.h file due to and I'm still receiving the:
由于我重新制作了lines.h文件,我仍然收到:
multiple definition of `draw_line(float, float, float, float)'
and
和
multiple definition of `near_far_clip(float, float, float*, float*, float*, float*, float*, float*)'
errors in the lines.cpp file, but the multiple definition of `stack'
error is now in a random place in the ThreeD.cpp file (and is marked by a comment now). Update:This error has been fixed and the files have been revised to show this:
lines.cpp 文件中的multiple definition of `stack'
错误,但错误现在位于 ThreeD.cpp 文件中的随机位置(并且现在由注释标记)。更新:此错误已修复,文件已修改以显示:
I messed around with labeling some the global variables extern, but it didn't seem to affect anything.
我搞砸了一些全局变量 extern 的标签,但它似乎没有影响任何东西。
采纳答案by Thomas L Holaday
Why do you #include lines.cpp in ThreeD.cpp? This is very unusual.
为什么在 ThreeD.cpp 中 #include lines.cpp?这是非常不寻常的。
Your makefile wants lines.o, so you're going to compile lines.cpp. Anything defined in lines.cpp will be in lines.o and also in ThreeD.o.
你的makefile 需要lines.o,所以你要编译lines.cpp。在lines.cpp 中定义的任何东西都将在lines.o 和ThreeD.o 中。
There is an intriguing comment in lines.cpp:
在lines.cpp中有一个有趣的评论:
Don't forget to put declarations in your .h files.
I think the instructor wants you to break lines.cpp into a .h and a .cpp.
我认为讲师希望您将lines.cpp 分解为.h 和.cpp。
Excerpt from lines.cpp:
摘自lines.cpp:
/* These go in your .h file or in lines.h */
/*
Line drawing header.
*/
void draw_line(float, float, float, float);
int near_far_clip(float, float, float *, float *, float *, float *,
float *, float *);
I suspect that these two declarations are the only thing that should be in lines.h.
我怀疑这两个声明是唯一应该在 line.h 中的东西。
回答by strager
You are likely including the function definitions in a header file. Include the inline
keyword so they are not exported by each object file, or put them in their own .cpp
file.
您可能将函数定义包含在头文件中。包括inline
关键字,这样它们就不会被每个目标文件导出,或者将它们放在自己的.cpp
文件中。
For your global variable, you need to use the extern
keyword in your header file. Otherwise, each object file exports their own variable and the linker gets confused as to which is the correct one to use.
对于全局变量,您需要extern
在头文件中使用关键字。否则,每个目标文件都会导出它们自己的变量,链接器就会混淆使用哪个是正确的变量。
回答by bayda
Check include guards on misspelling.
Check your make options, maybe someone compiled into multiple object files.
Try to exclude parts of files and code until not find cause of errors.
检查包括拼写错误的保护措施。
检查您的 make 选项,也许有人编译成多个目标文件。
尝试排除部分文件和代码,直到找不到错误原因。
EDITED:
fix include *.cpp files. They are should be linked.
编辑:
修复包含 *.cpp 文件。他们应该是有联系的。
回答by MrCranky
As has been said - not enough information to properly diagnose here.
如前所述 - 没有足够的信息来正确诊断这里。
But if draw_line
et al are defined in a header file rather than in a source (cpp) file, you may have methods which are supposedly declared as inline in the header file, which aren't actually properly inlining. In that case, each .cpp file that includes the header will generate their own definition of the draw_line
function, and generate warnings at link time.
但是,如果draw_line
et al 是在头文件中而不是在源 (cpp) 文件中定义的,则您可能拥有在头文件中声明为内联的方法,但实际上并没有正确内联。在这种情况下,每个包含标头的 .cpp 文件都会生成自己的draw_line
函数定义,并在链接时生成警告。
This can happen if you use a #defined INLINE macro which comes from a system header that has been forgotten or removed, and for whatever reason the INLINE gets pre-processed away into nothing.
如果您使用来自已被遗忘或删除的系统标头的 #defined INLINE 宏,则可能会发生这种情况,并且无论出于何种原因,INLINE 都被预处理为空。
E.g.:
例如:
//Lines.h
#define GCCC //Note the typo
#if defined(GCC)
#define INLINE inline
#elif defined (MSVC)
#define INLINE __inline
#else
#define INLINE //Due to the typo, INLINE will be turned into nothing
#endif
INLINE void draw_line(float x1, float y1, float x2, float y2)
{
//Draw the line
}
//File1.cpp
#include "lines.h" //will define draw_line
//File2.cpp
#include "lines.h" //will also define draw_line
And linking File1.cpp and File2.cpp will generate multiple declaration errors
并且链接 File1.cpp 和 File2.cpp 会产生多个声明错误
回答by Ferdinand Beyer
Please post some code snippets. Maybe you are defining your methods in both the class declaration and outside?
请发布一些代码片段。也许您在类声明中和外部都定义了方法?
class X {
void foo(); // No definition, just declaration
void bar() {} // Declaration + definition
};
void X::foo() {} // First Definition, OK
void X::bar() {} // Already defined, ERROR
回答by Gaurav Maan
#ifndef THREED_H_
#define THREED_H_
#endif /* THREED_H_ */
delete or comment these lines and this worked for me
删除或评论这些行,这对我有用
回答by Gaurav Maan
Multiple definition errors are linker errors, but without more information it's difficult to diagnose them. Check the same file does not appear twice in the linker command and that your header files contain only declarations, no definitions.
多个定义错误是链接器错误,但没有更多信息就很难诊断它们。检查同一个文件在链接器命令中没有出现两次,并且你的头文件只包含声明,没有定义。
回答by Konrad Rudolph
Without seeing the code there's really no helping you. The compiler clearly claims the opposite of you (there isat least one duplicate definition).
不看代码真的对你没有帮助。编译器清楚地要求于你的相反(有是至少一个重复的定义)。
Try reproducing the error with a minimum example. Have you tried compiling the code outside Eclipse in the command line? With what result?
尝试使用最小示例重现错误。您是否尝试过在命令行中在 Eclipse 之外编译代码?有什么结果?
回答by Konrad Rudolph
Hi, What I see here that the basic rule applies for ll. I will explain how the things work in these kind of errors and wht those comes : Let's take one example of"Making a global variable"
嗨,我在这里看到的基本规则适用于 ll。我将解释这些错误是如何工作的,以及这些错误是怎么回事:让我们举一个“制作全局变量”的例子
Dont confuse the header file, for a cpp file. The header is only there to allow multiple file projects to have a common interface before linking.
对于 cpp 文件,不要混淆头文件。标头仅用于在链接之前允许多个文件项目具有通用接口。
1.List item
1.列表项
You should give info about the global variable in header:
您应该在标题中提供有关全局变量的信息:
extern int Var_Global;
extern int Var_Global;
List item
while keeping the Actual code in the cpp : int Var_Global;
项目清单
while keeping the Actual code in the cpp : int Var_Global;
And make sure you 1. include the header, and 2. link the code.
并确保您 1. 包含标题,以及 2. 链接代码。
Link the code once, and once only, and there is only one copy of the data, howver you can include headers all day long, this tells the other code that there will be a copy of the data at link time.
将代码链接一次,并且只链接一次,并且只有一个数据副本,但是您可以整天包含标题,这告诉其他代码在链接时将有数据的副本。
I see a lot of people putting code in header files, while that may work in some cases, it is a bad habit to get into, and will cause problems sooner or later.
我看到很多人把代码放在头文件中,虽然在某些情况下可能会起作用,但这是一个坏习惯,迟早会出问题。
Regards,
问候,
Prashanta
普拉尚塔