windows 为什么错误 LINK2005:当我将对象声明为静态时,对象已定义错误消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4182866/
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
Why error LINK2005: object already defined error disappears when I declare the object as static
提问by Simsons
I have the follwoing structure and object of structure defined in the header file as below:
我在头文件中定义了以下结构和结构对象,如下所示:
struct STConfigurationDetails
{
bool bAutoStart;
bool bAutoLog;
bool bAutoScan;
bool bAutoMount;
bool bAutoOpen;
bool bAutoDetectLast;
};
struct STConfigurationDetails g_objConfigurationDetails ;
In the header file it self I have both method and method body which is using g_objConfigurationDetails . This works fine when I include the header file to another cpp file and call the method. But the moment I added the header file to another cpp file I got the error:
在它自己的头文件中,我有使用 g_objConfigurationDetails 的方法和方法主体。当我将头文件包含到另一个 cpp 文件并调用该方法时,这很好用。但是当我将头文件添加到另一个 cpp 文件时,我收到了错误:
Error 1 error LNK2005: "struct STConfigurationDetails g_objConfigurationDetails" (?g_objConfigurationDetails@@3USTConfigurationDetails@@A) already defined in NDSClientDlg.obj NDSConnectDlg.obj NDSClient
Error 2 fatal error LNK1169: one or more multiply defined symbols found d:\FromClearCase\Development_view\NDS_11152010\exe\Debug\NDSClient.exe 1 NDSClient
错误 1 错误 LNK2005:“struct STConfigurationDetails g_objConfigurationDetails”(?g_objConfigurationDetails@@3USTConfigurationDetails@@A) 已在 NDSClientDlg.obj NDSConnectDlg.obj NDSClient 中定义
错误 2 致命错误 LNK1169:发现一个或多个多重定义的符号 d:\FromClearCase\Development_view\NDS_11152010\exe\Debug\NDSClient.exe 1 NDSClient
After searching few threads I found out I have to declare my object as static and it solved. But I want to know why was I getting multiple instance error while I was creating the instance only in te header file.
在搜索了几个线程后,我发现我必须将我的对象声明为静态对象并解决了。但是我想知道为什么我只在头文件中创建实例时会出现多个实例错误。
Is this because my Header File has a global variable and it is being included in multiple CPPs?
这是因为我的头文件有一个全局变量并且它被包含在多个 CPP 中吗?
回答by kichik
Adding static
might solve your linking problem, but gives you a much bigger problem. That variable is no longer global and has a different value in every CPP file that uses it. You need to declare it as extern
in the header file and then declare it one more time in just one CPP file as is.
添加static
可能会解决您的链接问题,但会给您带来更大的问题。该变量不再是全局变量,并且在使用它的每个 CPP 文件中都有不同的值。您需要extern
在头文件中声明它,然后在一个 CPP 文件中再声明一次。
When you use static
it means the variable will be completely local to the current CPP file and will not be exposed to other files. That's why the linker no longer cares if there is another static variable in another file that has the same name. They are not the same variable.
当您使用static
它时,意味着该变量将完全位于当前 CPP 文件的本地,并且不会暴露给其他文件。这就是为什么链接器不再关心另一个文件中是否有另一个同名的静态变量。它们不是同一个变量。
If you want a truly global variable, it must be declared in exactly one CPP file and only its prototype (with extern
) should be in a header file that will be shared with other CPP files. It's exactly like functions - declared in one file, prototyped for the rest. For functions, you simply don't provide a body. For variables, you use extern
.
如果你想要一个真正的全局变量,它必须在一个 CPP 文件中声明,并且只有它的原型(with extern
)应该在一个将与其他 CPP 文件共享的头文件中。它就像函数一样——在一个文件中声明,为其余文件设计原型。对于函数,您根本不提供主体。对于变量,您使用extern
.
回答by sharptooth
This is quite easy if you think of it carefully. The variable is defined in the header so each .cpp file that includes that header gets its own copy of the variable. Now if you don't add static
all the .cpp files get the same variable with external linkage and an error occurs at compile-time.
如果你仔细想想,这很容易。该变量在标头中定义,因此每个包含该标头的 .cpp 文件都会获得它自己的变量副本。现在,如果您不添加static
所有 .cpp 文件,则使用外部链接获取相同的变量,并且在编译时会发生错误。
When you add static
each .cpp still has its variable unrelated to other variables from the same definition but they no longer have external linkage so the linker doesn't emit an error.
当您添加static
每个 .cpp 时,它的变量仍然与来自同一定义的其他变量无关,但它们不再具有外部链接,因此链接器不会发出错误。
However don't forget that each variable is a separate variable that occupies memory and has overhead for construction/destruction and you will get unexpected behavior if you code expects to have only one variable shared across all .cpp files.
但是不要忘记每个变量都是一个单独的变量,它占用内存并有构建/销毁的开销,如果您的代码希望在所有 .cpp 文件中只共享一个变量,您将获得意外行为。
回答by Bj?rn Pollex
Global static variables have internal linkage.
全局静态变量具有内部链接。