![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 18
Rep Power: 0
![]() |
static class members and libraries (linking question)
OK I have to apologize in advance for this obscure question, but hopefully somebody can point me in the right direction...
Consider a class with a static member, initialized in that class' header file. i.e., // Obj.h
class Obj
{
...
static int num;
...
};
Obj::num = 3;Now assume this header is included in various places of an application, as well as runtime libraries loaded by that application. In terms of the entire process, will there exist one single Obj::num, or will each module have its own copy of Obj::num? I've been doing related testing, and I'm confused because I've seen both behaviors, but I can't narrow down what causes each. I've been trying different ways of building shared libraries (i.e., using ld myself, using g++ -shared, etc.). Is there a C++ language mechanism I can use to explicitly force the "single copy for the entire process" scenario, despite how the application and its modules are compiled/linked? For instance, changing my header to: // Obj.h
class Obj
{
...
static int num;
...
};
#ifdef MAIN_MODULE
Obj::num = 3;
#endifI presume would only declare num in my main module (assuming I define MAIN_MODULE for that compilation), but I'm not familiar enough with what's happening behind the scenes to know if this is correct. I would like a way to code this so I don't have to worry about how it is compiled and linked on a bunch of different UNIX platforms. Any insight (or directions to any related linking tutorials) is appreciated... |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
Good question! The answer is that the library has no choice but to guarantee that the definition of a static member is performed only once. If a static member is defined multiple times, the behavior is undefined. As with anything falling under the One Definition Rule, the easiest fix is to provide a header with your declarations for inclusion in however many files you want:
#ifndef C_H
#define C_H
class C {
...
static int data;
...
};
#endif#include "c.h" int C::data = 0; ![]() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 18
Rep Power: 0
![]() |
Thanks a lot eggbert.
That clears things up quite a bit. The whole multiple definition being undefined thing clears up the different behavior I've been seeing. So I'm assuming using something like that compile flag would have the same effect as moving the definition to a source file that gets linked into the main module (and no other modules in the application), so long as it's only defined once. Thanks again. EDIT: I forgot to ask if you or anybody happened to know where I could find this documented? Or anything related to the "one definition rule" in general. Thanks. Last edited by earl; Jun 22nd, 2005 at 5:55 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|