如何避免头文件中定义的变量出现 LNK2005 链接器错误?
我有 3 个 cpp 文件,看起来像这样
#include "Variables.h"
void AppMain() {
//Stuff...
}
他们都在其中使用相同的变量,因此它们具有相同的标头,但我得到这样的东西
1>OnTimer.obj : error LNK2005: "int slider" (?slider@@3HA) already defined in AppMain.obj
这是为什么?
I have 3 cpp files that look like this
#include "Variables.h"
void AppMain() {
//Stuff...
}
They all use the same variables inside them so they have the same headers but I get stuff like this
1>OnTimer.obj : error LNK2005: "int slider" (?slider@@3HA) already defined in AppMain.obj
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请记住,#include 大致就像将包含的文件剪切并粘贴到包含它的源文件中(这是一个粗略的类比,但您明白了)。这意味着如果您在头文件中有:
并且该头文件由程序中的三个源文件包含,那么它们都将定义一个名为 x 的全局变量,这将发生冲突。
您想要做的是将变量定义为 extern,以便 .cpp 文件都获得声明,然后在其中一个 .cpp 文件中给出实际的定义。
在 Variables.h 中:
在 SomeSourceFile.cpp 中
当然,我建议不要使用全局变量,但如果您必须使用它们,这将防止它们发生冲突。
Keep in mind that a #include is roughly like cutting and pasting the included file inside the source file that includes it (this is a rough analogy, but you get the point). That means if you have:
in the header file and that header file is included by three source files in a program, then they will all have a global named x defined that will conflict.
What you want to do is define the variable as extern so that the .cpp files will all get the declaration, and then in ONE of your .cpp files give the actual definition.
in Variables.h:
in SomeSourceFile.cpp
Of course, I'd recommend against globals, but if you must use them this would keep them from conflicting.
这是因为编译器单独编译每个
.cpp
文件,为每个文件创建一个.obj
文件。您的标头似乎包含以下内容:当将其包含到您的三个
.cpp
文件中的每一个中时,您将获得int slider
的三个 副本变量,就像您在每个.cpp
文件中声明它一样。链接器会抱怨这一点,因为你没有三个不同的东西具有相同的名称。您可能想要做的是将头文件更改为:
这告诉编译器某处有一个
slider
变量,但可能不在这里,并让链接器找出它。然后,在一个.cpp
文件中:为链接器提供一个要链接的实际变量。
This is because the compiler compiles each
.cpp
file separately, creating a.obj
file for each one. Your header appears to have something like:When this is included into each of your three
.cpp
file, you get three copies of theint slider
variable, just as if you had declared it in each.cpp
file. The linker complains about this because you haven't have three different things with the same name.What you probably want to do is change your header file to read:
This tells the compiler that there is a
slider
variable somewhere, but possibly not here, and lets the linker figure it out. Then, in one.cpp
file:gives the linker one actual variable to link.
因为“int slider”已经在另一个文件中定义了?检查您是否有标头保护...
如果它跨多个翻译单元,并且您确实希望变量不同(即不是全局的),那么也许可以在匿名命名空间中声明它们:
如果您确实希望它们是全局的,请查看詹姆斯的解决方案。
Because "int slider" is already defined in another file? Check that you have header guards...
If it is across multiple translation units, and you do want the variables to be different (ie not global), then maybe declare them in an anonymous namespace:
If you do want them global, look to James' solution.
发生的情况是,Variables.h 中的每个变量都被赋予每个单独的 c 文件的全局范围。当链接器编译所有 c 文件时,它会看到多个具有相同名称的变量。
如果您想使用头文件中的变量作为全局变量,那么您必须在所有变量前面使用关键字“extern”,并且在主文件中不要使用关键字 extern。
main c:
其他文件:
您可以创建两个文件Variables.h和EVariables.h,或者只在main.cpp文件中声明变量。
更好的方法是创建一个变量类并传递对该类的引用。
What is happening is that each of the variables from Variables.h are given global scope for each of the individual c files. When the linker compiles all the c files, it sees multiple variables with the same name.
If you are wanting to use variables from the header file as global variables, then you will have to use the keyword "extern" in front of all of them, and in the main file don't use the keyword extern.
main c:
other files:
You can create two files Variables.h and EVariables.h, or just declare the variables in the main.cpp file.
A much better way to do this is to create a class of Variables and pass a reference to the class.
我知道这是一个旧线程,但我发现这是谷歌的第一个搜索结果之一。我通过将变量静态化解决了这个问题。
我尝试了 extern,但在我的情况下似乎没有解决问题。
I know that this is an old thread, but I came across this as one of the first search results from Google. I solved the problem by placing the variable static.
I tried extern and in my situation that didn't seem to solve the problem.
如果通过“Variables.h”多次包含的变量声明为 const,也可以避免此链接错误。
This linking error can also be avoided if the variables included multiple times via the "Variables.h" are declared as const.
尽管我使用外部定义,但我也遇到了这个错误。问题也在于初始化外部定义中的变量:
=>错误
=>没有错误,问题解决
I had this error too although I work with extern definitions. The problem was initializing the variables in the extern definitions too:
=> error
=> no error, problem solved