egcs-1.0.2: bug with static initializers and shared libs?

David Lucas dlucas@checkfree.com
Thu May 7 20:49:00 GMT 1998


Tobias Gloth wrote:
> --- foo.h ---
> #include <iostream.h>
> 
> struct Foo {
>     Foo () { cout << "foo!" << endl; }
> };
> 
> --- bar.h ---
> #include "foo.h"
> 
> template<class T>
> struct Bar {
>     static Foo* foo () {
>         static Foo f;
>         return &f;
>     }
> };
> 
> --- inst1.cpp ---
> #include "bar.h"
> 
> static Bar<int> bar;
> 
> void inst1 () {
>     cout << "inst1" << endl;
>     bar.foo();
> }
> 
> --- inst2.cpp ---
> #include "bar.h"
> 
> static Bar<int> bar;
> 
> void inst2 () {
>     cout << "inst2" << endl;
>     bar.foo();
> }
> 
> --- app.cpp ---
> extern void inst1();
> extern void inst2();
> 
> int main () {
>     inst1();
>     inst2();
>     return 0;
> }
> 
> I use these commands to create the executable:
> 
>     $ c++ -g -Wall -c app.cpp
>     $ c++ -fPIC -g -Wall -c inst1.cpp
>     $ c++ -shared -o libinst1.so inst1.o
>     $ c++ -fPIC -g -Wall -c inst2.cpp
>     $ c++ -shared -o libinst2.so inst2.o
>     $ c++ -o app app.o -L. -linst1 -linst2
> 
> Note that I'm building shared libraries. Ok, the output is:
> 
>     $ ./app
>     inst1
>     foo!
>     inst2
>     foo!
> 
Since you have two seperate files with a static Bar<int>
bar; in them, seems as though each file has its own file
scope to be initialized.  Why would it not be initialized
for each file?  Secondly, they are placed in shared
libraries.  Each shared library fires a static space
initializer for static constructors.  I don't think the spec
says this should not happen, but all statics be initialized
after loading of the library.  So, each library that loads
would initialize each file scoped static Bar, thus each
foo.  Does that put more mud in the puddle?

Seems as thought it is behaving like it should.

Later,
Dave



More information about the Gcc-bugs mailing list