Specify shared library search path at runtime



  • Hello everybody,

    I have an application which load a bunch of plugins, and one of these plugins depends from a shared library (dylib or dll..). If I start the program in the folder where the library is, everything is ok. But if I start the program elsewhere.. I get a crash because one of the plugins failed to load (as its shared library cannot be found).

    See this snapshot for a simple example: http://cl.ly/3w30220S1c3z1g300p3c

    Now I tried to set my PATH environment variable to point to the place where the library is:
    @
    export PATH="$PATH:$PWD"
    @
    Not working...

    I also tried to use the QCoreApplication::addLibraryPath:
    @
    QString aPath = applicationDirPath();
    this->addLibraryPath(aPath);
    @
    Not working either...

    Or to attempt to load the library manually at runtime:
    @
    QDir baseDir(applicationDirPath());
    QString libAbsPath(baseDir.absolutePath()+"/libdynamic_surfacemesh.dylib");
    qDebug() << libAbsPath;
    QLibrary lib(libAbsPath);
    lib.load();
    qDebug() << lib.errorString();
    @
    Again... not working, as the output of the qDebug above is:
    @
    ata2 AT pluto ~/Developer/starlab-build/distrib/starlab.app/Contents: ./MacOS/starlab
    "/Users/ata2/Developer/starlab-build/distrib/starlab.app/Contents/MacOS/libdynamic_surfacemesh.dylib"
    "Unknown error"
    @

    Any suggestion? Thank you very much
    (Note that "installing" is not an option. I don't want the user to need root privileges to install/run this application.)


  • Moderators

    That depends a lot on the platform you are on.

    1 should work on windows.

    Check "man ld" and "man dyld" on Linux/Mac for details on how to influence the library loading at runtime.



  • Hi Tobias, thanks for the quick answer. Shouldn't Qt offer a platform independent way of doing it though? That's what I was hoping for...



  • Thanks for the pointer above though...
    "DYLD_LIBRARY_PATH" works in OSX :) (and hopefully in linux)

    I guess in windows command line execution is not that meaningful considering their terminal is complete garbage...



  • [quote author="tallia1" date="1321170761"]Hi Tobias, thanks for the quick answer. Shouldn't Qt offer a platform independent way of doing it though? That's what I was hoping for...[/quote]

    Although that would be nice, it is too late when Qt enters the game. That library path juggling is handled by the OS and the loader and the env vars must be set before the program starts.



  • On Windows, it is possible to modify the dll search path inside the program, but it only works for dynamically loaded modules, as all others are loaded before main.

    If you create some loader app(exe that sets search paths and then loads the main module as dll) it would work: SetDllDirectory



  • Mhh, that's not a bad idea.. it's like a fake application. Maybe I can do it in a (platform dependent) script and execute it within the application...

    Yet.. is there a way of dynamically loading the libraries after the program starts? (with the same effects as if they were in the exe folder...). I would have though QLibrary did that, but doesn't seem to be working... I tried to point it to my dylib but the isLoaded() gave me "false".



  • You can load libraries after the program start only if the program is not "hard" linked against those and does not need them to start up. This is false for all the Qt libs, it usually works for plugins. QLibrary is just a wrapper around the native dlopen/LoadLibrary functions to ease the latter.

    You can do some tricks if you don't know exactly what version you are loading. Have a look at Qt's SSL support classes, they late-load the SSL libraries, as there are license issues with OpenSSL.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.