Why error LINK2005: object already defined error disappears when I declare the object as static

前提是你 提交于 2020-06-23 07:43:26

问题


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:

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

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?


回答1:


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.

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.

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.




回答2:


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.

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.

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.




回答3:


Global static variables have internal linkage.



来源:https://stackoverflow.com/questions/4182866/why-error-link2005-object-already-defined-error-disappears-when-i-declare-the-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!