Global (static) Var in a library initialization question.

K

Kyle

Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

Thanks!

If you run sbin (linking with .a), the output is 0 however if
you run shbin(linking with .so) the output is one.
Tried with GCC 3.4

///////////////////////////////////////////
//lib.h
#include <iostream>
#include <string>
#include <vector>

template <class T>
class SingleTon
{
public:
static T * instance ();

protected:

SingleTon () {}
static T * instance_;
};

class A : public SingleTon<A>
{
public:
A () {}
int size () { return box_.size (); }
void push (int i) { box_.push_back (i); }
int pop ()
{
int back = box_.back ();
box_.pop_back ();
return back;
}
protected:
std::vector<int> box_;
};

template <class T>
T * SingleTon <T>::instance_ = 0;

template <class T>
T * SingleTon <T>::instance ()
{
if (!instance_) instance_ = new T;
return instance_;
}

/////////////////////////
//test.h
#include "lib.h"

class B
{
public:
B ();
};

//////////////////////////////
//test.cpp
#include "test.h"

B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"

int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());
}

#////////////////////////////////
#Makefile
all : sbin shbin

test.o : test.h test.cpp lib.h
g++ -g -c test.cpp -o test.o

libtest.a : test.o
ar rsuv libtest.a test.o

libtest.so : test.o
g++ -g -shared -o libtest.so test.o

sbin : libtest.a
g++ -g main.cpp -o sbin ./libtest.a

shbin : libtest.so
g++ -g main.cpp -o shbin ./libtest.so

..PHONY : clean
clean :
rm *.o *.so *.a sbin shbin
 
V

Victor Bazarov

Kyle said:
I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

Look in the FAQ for "fiasco"

V
 
K

Kyle

Hi Victor,

Thanks for the help. Actually I thought about the fiasco order problem
when I was working on the orignal problem which lead to this simplified
code snip, however, I don't think that this is the same case.

In my code there is no dependency amoung two object files unless you
count the one which contains no global var but only main func. Maybe I
am missing something.

Thanks!
 
G

gottlobfrege

Kyle said:
Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

//////////////////////////////
//test.cpp
#include "test.h"

B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"

int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());
}

Is the rule is that B b; must be initialized before any function in
test.cpp is called, or before main() is called? Note that there are no
functions in test.cpp to be called. I'm tempted to think that the
compiler/linker could throw out test.cpp completely...

Tony
 
V

Victor Bazarov

Thanks for the help. Actually I thought about the fiasco order problem
when I was working on the orignal problem which lead to this simplified
code snip, however, I don't think that this is the same case.

In my code there is no dependency amoung two object files unless you
count the one which contains no global var but only main func. Maybe I
am missing something.

You're right, I ought to look a little more carefully. Have you tried
putting some side effect (besides calling a function in A) into the B's
constructor? Does it actually get called?

V
 
M

mlimber

Kyle said:
Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

Technically, issues related to static libraries are an operating
system/tools problem and should be taken to the appropriate forum for
your tools.
Thanks!

If you run sbin (linking with .a), the output is 0 however if
you run shbin(linking with .so) the output is one.
Tried with GCC 3.4

///////////////////////////////////////////
//lib.h
#include <iostream>
#include <string>

You don't use these two headers in this file. Don't include them here.
#include <vector>

template <class T>
class SingleTon
{
public:
static T * instance ();

protected:

SingleTon () {}
static T * instance_;
};

class A : public SingleTon<A>
{
public:
A () {}
int size () { return box_.size (); }
void push (int i) { box_.push_back (i); }
int pop ()
{
int back = box_.back ();
box_.pop_back ();
return back;
}
protected:
std::vector<int> box_;
};

template <class T>
T * SingleTon <T>::instance_ = 0;

template <class T>
T * SingleTon <T>::instance ()
{
if (!instance_) instance_ = new T;
return instance_;
}

/////////////////////////
//test.h
#include "lib.h"

You don't use this header in this file, so don't include it here. It
can speed up compile times to include the minimal amount, especially on
large projects.
class B
{
public:
B ();
};

//////////////////////////////
//test.cpp
#include "test.h"

Add:
#include "lib.h"
B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"

Add:
#include said:
int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());

Ewww. Prefer the type-safe std::cout over std::printf.
[snip]

First, I would suggest renaming your class from SingleTon (which most
would interpret as a solitary heavy-weight) to Singleton, the
conventional designation for the design pattern.

Second, note that inheriting Singleton<A> does not make A a singleton
because the user can still easily create an independent instance or
copy the singleton instance, meaning there can be more than one A.
E.g.,

A a1( *A::instance() );
A a2;

Check out _Modern C++ Design_ chapter 6 for more than you ever wanted
to know about singletons. The book discusses some pitfalls and tricks
to creating singletons. You can download the library code from:

http://sourceforge.net/projects/loki-lib/

Check out Singleton.h.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top