No linker error when global variable declared static in the header file [duplicate]

六眼飞鱼酱① 提交于 2021-02-19 03:46:48

问题


Possible Duplicate:
Static variables in C++

// x.h
int i = 3;

// x1.cpp
#include"x.h"
//...

// x2.cpp
#include"x.h"
//...

Above code will give linker error. However If I declare,

//x.h
static int i = 3;

It doesn't give linker error in gcc, even we have the same #include! Are we creating different static int i; for every .cpp file ? Will it cause any silent linking bug (due to same name)?


回答1:


Are we creating different static int i; for every .cpp file ?

Yes

Will it cause any silent linking bug (due to same name)?

No. Due to static, they have different names.

If this isn't the behavior you want, you need to use extern in the header file, and allocate the variable in one translation unit (.cpp file)




回答2:


When C code is compiled, it's one "translation unit" at a time. Early on, #includes are expanded into the text of the referenced files. So what you've got in the static case is equivalent to x1.cpp saying static int i = 3; and x2.cpp doing the same. And static in this context means roughly "don't share this with other translation units."

So yes, when you use static there you are making two different i variables which have nothing to do with each other. This will not cause a linking error.




回答3:


int x; is a definition of the entity x. The One Definition Rule of C++ says that any variable that is used shall be defined exactly once in the program. Hence the error.

static says that x has internal linkage. That is, the x's that appear in one.cpp and two.cpp are two different unrelated entities.

The C++ standard says that the use of static in this case is deprecated(As per Steve's comment, in C++0x it's undeprecated). Anonymous namespaces provide a superior alternative.

namespace
{
   int x;
}

Also note that unlike C, in C++ const variables of scalar types also have internal linkage. That is

const int x = 7; // won't give you an error if included in different source files.

HTH




回答4:


static creates a global variable that is only visible inside the unit.

If you want to use a variable in more than on ecompilation unit, use extern in the header and declare it in the implmenetation without extern.




回答5:


You get the linker error in your first code example because i is defined and exported in both compilation units. In the second case i is static, so there is no exported symbol because static variables are only visible in the current compilation unit and aren't exported to the linker. In this case you have two independent variables that are both called i.




回答6:


As written, the code looks like the same i is being accessed by multiple .cpp files, whereas in reality, each .cpp file will have its own copy. This can lead to misunderstandings and bugs.

If you want there to be just one copy of i, the preferred idiom is to wrap it in an accessor function in x.h:

int& GetI() {
  static int i = 3;  // this initialization only happens once.
  return i;
}

If you do want separate copies of i for each .cpp file, a much clearer expression of this is to simply declare i separately in each .cpp file:

namespace {
  int i;
}

Putting it in an anonymous namespace as above keeps it from being accessible from other .cpp files, for safety.



来源:https://stackoverflow.com/questions/6725130/no-linker-error-when-global-variable-declared-static-in-the-header-file

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