Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 22nd, 2005, 2:58 PM   #1
earl
Newbie
 
Join Date: Jun 2005
Posts: 18
Rep Power: 0 earl is on a distinguished road
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;
#endif

I 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...
earl is offline   Reply With Quote
Old Jun 22nd, 2005, 3:31 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
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
And then a source file that is linked with the project once and provides definitions for declared names in the header file:
#include "c.h"

int C::data = 0;
This is a safe and easy way to avoid multiple definitions, especially for situations where an error or warning is not required, such as this one.
Eggbert is offline   Reply With Quote
Old Jun 22nd, 2005, 5:21 PM   #3
earl
Newbie
 
Join Date: Jun 2005
Posts: 18
Rep Power: 0 earl is on a distinguished road
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.
earl is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC