By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

Global (static) Var in a library initialization question.

P: n/a
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

Nov 10 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Kyle wrote:
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
Nov 10 '05 #2

P: n/a
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!

Nov 10 '05 #3

P: n/a

Kyle wrote:
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

Nov 10 '05 #4

P: n/a
On Wed, 09 Nov 2005 21:51:00 -0800, Kyle wrote:
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
Nov 10 '05 #5

P: n/a
Kyle wrote:
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 <iostream>

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

Nov 10 '05 #6

P: n/a
Tony,

You are right, the test.o is not linked in at all.....
Thanks!

-Kyle

Nov 10 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.