g++/static members/DLL problem

Stephane Corbe sc@axlog.fr
Tue Mar 12 01:38:00 GMT 2002


Kohn Emil Dan wrote:

> Hello everyone,
>
> I would like to report a problem with g++ static member initialization on
> cygwin. I have a class in which I have a static data member. The
> definition of that class resides in a DLL. That static member is
> initialized in the corresponding .cpp file. However when I access it from
> an application, I do not get the initialized value, instead I get just
> garbage.
>

This is the case for all the global variables (and static members are global).
By the way, it's not a good idea to share other things than pointers,
Dll can shre only pointers. Here you have no warning message because
your variable (int) has the same size than a pointer.
Try to share an object to see.

A solution is to do :

tbar *Foo_bar;

class Foo {
public:
    static void init();
    static void done();
};

void Foo::init()
{
   Foo_bar = new tbar(17);
}

void Foo::done()
{
  delete Foo_bar;
}

int main()
{
   Foo::init();

   printf("Foo::bar=%d\n", *Foo_bar);

   Foo::done();
   return 0;
}




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list