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
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