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

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

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.

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

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?

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>

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

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: