PDA

View Full Version : static objects in libraries



Rawk
14th January 2007, 15:40
Hi,
I'm currently writing a program which uses plugins. I've written two libs which are used by the program and the plugins. There are some static objects in the libs. On Linux these objects are shared across the program and the plugins (as I wantet it), but on windows the static objects are different for the program and each plugin.
When I built the libs as shared libs on Windows the program rejects to start and gives an initialization error instead. On Linux it works no matter whether I build the libs as shared or static libs. Any idea what could be wrong or what I can try?

camel
14th January 2007, 16:40
I think using static variables across library boundaries is something one should rather avoid. (Googling should give you some material, something like initialization is not asured for example).

I would try to use static variables inside a wrapper function.


int &SomeClass::staticInt()
{
static int realInt;
return realInt;
}

(Or check how its done in Qt using the Q_GLOBAL_STATIC macro..which is neither documented, nor public api in the moment as far as I know)

Rawk
16th January 2007, 16:57
I think using static variables across library boundaries is something one should rather avoid. (Googling should give you some material, something like initialization is not asured for example).
Maybe I've used the wrong words, but I didn't find sth useful.



I would try to use static variables inside a wrapper function.


int &SomeClass::staticInt()
{
static int realInt;
return realInt;
}

(Or check how its done in Qt using the Q_GLOBAL_STATIC macro..which is neither documented, nor public api in the moment as far as I know)
Both approaches didn't really change the current behaviour. Is the only solution to transfer pointers of the objects that need to be exactly the same in the app and the plugins through the plugin interface?
I'm still wondering why it works on Linux, but not on Windows.
Another problem that comes to my mind now: on Windows qobject_casts of pointers from the plugins in the app don't work, but dynamic_casts do. I'm using gcc-3.4.6 on Linux and the current stable MinGW (gcc-3.4.2) on Windows.

Moppel
16th January 2007, 19:03
Another problem that comes to my mind now: on Windows qobject_casts of pointers from the plugins in the app don't work, but dynamic_casts do. I'm using gcc-3.4.6 on Linux and the current stable MinGW (gcc-3.4.2) on Windows.
I've got a similar problem as described in:
http://www.qtcentre.org/forum/f-qt-programming-2/t-casting-result-of-qwidgetstack-currentwidget-5037.html
I haven't found a solution for Windows yet but this thread describes the backround quite will.