Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using a static lib's static variable inside of a plugin



  • So we have our own custom logging mechanism. This mechanism is accessed through a static member variable of the logging class. The logger is compiled as part of a static lib.

    We are also moving towards a plugin architecture and would like to make use of the logger inside of the plugins. However, accessing the logger's static member variable returns a null ptr from within the plugin code. Accessing the logger from any app or static lib code works as expected.

    I'm guessing this has to do with how plugins are loaded and the fact that they don't immediately share the same memory space as the app.

    Is there a way to achieve my goal or do we have to redesign how our logger works?


  • Lifetime Qt Champion

    Hi,

    Are your plugins built against your static library ?



  • @SGaist As in, am I statically compiling the static libs into the plugin? I do not believe I am. Also, the plugins are not being statically compiled into my app but loaded at run-time.


  • Lifetime Qt Champion

    What I meant is literally what I wrote: are your plugins linked against that static library ? There's no concept of "shared linking against a static library".



  • Forgive my ignorance. In my plugin's project file, I have defined

    LIBS += -llib1 -llib2...
    DEPENDPATH += lib1 lib2..
    PRE_TARGETDEPS += lib1.a lib2.a...
    

    So I think so? My plugins build completely, so I'm guessing all symbols are resolved correctly.

    Let me know if you need more info



  • So I was able to fix this myself by wrapping access to the class's static variable with a static function.

    I have a feeling my usage of macros was causing part of the problem. Some of the macros were using the static member variable, and perhaps this was causing a separate translation unit in the plugin? I still don't quite understand what was happening underneath. Any explanation will be greatly appreciated.


Log in to reply