Results 1 to 4 of 4

Thread: static objects in libraries

  1. #1
    Join Date
    Jan 2007
    Posts
    9

    Question static objects in libraries

    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?

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static objects in libraries

    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.

    Qt Code:
    1. int &SomeClass::staticInt()
    2. {
    3. static int realInt;
    4. return realInt;
    5. }
    To copy to clipboard, switch view to plain text mode 
    (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)

  3. #3
    Join Date
    Jan 2007
    Posts
    9

    Default Re: static objects in libraries

    Quote Originally Posted by camel View Post
    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.

    Quote Originally Posted by camel View Post
    I would try to use static variables inside a wrapper function.

    Qt Code:
    1. int &SomeClass::staticInt()
    2. {
    3. static int realInt;
    4. return realInt;
    5. }
    To copy to clipboard, switch view to plain text mode 
    (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.

  4. #4
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: static objects in libraries

    Quote Originally Posted by Rawk View Post
    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-p...dget-5037.html
    I haven't found a solution for Windows yet but this thread describes the backround quite will.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. having both static and dynamic libraries?
    By gfunk in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2007, 07:33
  3. Qt 3.3 libraries
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2006, 17:25
  4. Static Compile [Qt4]
    By PieaholicX in forum Newbie
    Replies: 11
    Last Post: 1st September 2006, 17:18
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.