1

I am new to C++ linux and i have question how this is working,

header.h

static int arr [2] [2] = {some values}

lib.cpp - includes the header.h file and creates an .so

source.cpp - includes the same header.h file(and dynamicaly links lib.so) and while linking, throws an .gnu.linkonce_t error

So, to avoid that, I went with this approach,

header.h

class x {

static int arr [2] [2];
}

lib.cpp - includes the header.h file and creates an .so

source.cpp -

int x::arr = {define some values}

includes the same header.h file and does NOT throw an link error

xtofl
  • 40,723
  • 12
  • 105
  • 192
guru
  • 11
  • 1
  • 3

2 Answers2

2

First of all you should never define a static global variable in a header file. Defining a variable static outside of a class has a completely different meaning than defining it static inside of a class. That is very confusing but it's how C++ works.

There are at least four usages of the keyword static in C++:

1) A static global variable outside of a class will only be global to code that can directly "see" the variable normally because they are in the same file. This makes the variable local to the file. As mentioned above you should not put one of these in a header. If you do you will get multiple instances of the same variable. One in each file that includes the header. Changes to one will not affect the others in other files.

2) A static global function has much the same meaning as a static global variable in #1. In general it means the the function will only be called from within the file it is declared in. Like the static global variable if you implement a static global function in a header file you will end up with multiple copies of it in your final executable. However, unlike a static global variable if you put a static global function definition in a header file you will have trouble linking the resulting object files.

3) A static variable in a class means that there will only be one copy of that variable for all instances of the class.

4) A static member function in a class will only be able to access static variables with in that class. All other class data will be inaccessible from a static member function. However, you can call a static member function without a class instance. e.g. A::func().

What you probably want is either to use a static class variable or put a extern definition in a header file like this:

extern int arr[2][2];

Then in an implementation file some where actually create the variable instance, as a non-static variable like this:

int arr[2][2] = {some values};

Make sure you understand the difference between declaration/definition and instantiation/implementation. These are keep concepts in C/C++.

jcoffland
  • 5,238
  • 38
  • 43
1

Seams like you don't know what happens when you declare a static variable in a header.

Read more about it here.

Your solution with placing the static array in the class is fine, but under some circumstances could cause static order initialization fiasco, so you have to be careful when you use static class member variables.

If you prefer the 1st way, you could declare the array like extern variable, and define it in the source file.

Community
  • 1
  • 1
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • thanks much ... I also did try doing what you mentioned. But not sure which one is a better approach, header1.h --- extern int arr[2] [2]; and define the value in source.cpp ---- int arr [2] [2] = {some values} – guru Dec 14 '10 at 12:06
  • @guru IMO, the 2nd approach is better, because it is a bad practice to pollute the global namespace, and it is also bad practice to use global variables. – BЈовић Dec 14 '10 at 12:16
  • Thanks very much VJo ...... When i did the first one, static array had a external linkage and the compiler found an problem while linking the my .so obj file with my exe. But when i moved my arr to the class, it took an external linkage and everything worked alright. Hope my understanding is correct !!! – guru Dec 14 '10 at 15:39