C++ How linkage happens between a static variable in two different projects
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 d开发者_如何学JAVAynamicaly 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
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++.
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.
精彩评论