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

Tobias Gloth gloth@ESCHER.UNI-MUENSTER.DE
Thu May 7 10:16:00 GMT 1998


I'm having a problem when initializing static variables with return
values of functions, using c++. My system is:

    $ uname -a
    IRIX64 lilakuh 6.2 06101031 IP28
    $ c++ --version
    egcs-2.90.27 980315 (egcs-1.0.2 release)
    $ ld -v
    ld: INFO 61: Version 7.10.
    ld: INFO 46: No objects linked.

egcs was compiled without non-standard options, and it uses the native
linker. The Problem arises with the following files:

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

IMHO there should only be the first line "foo!". I get the expected
result when I'm using static libraries, or the native compiler on Irix,
or g++ 2.8.1 under Solaris.

Any comments are appreciated!

    -Tobias

-- 
Tobias Gloth    |  WWW:   http://www.westfalen.de/unknown
Kuhstr. 13      |  home:  gloth@unknown.westfalen.de
48143 Muenster  |  work:  gloth@math.uni-muenster.de



More information about the Gcc-bugs mailing list