Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Specify shared library search path at runtime
Forum Updated to NodeBB v4.3 + New Features

Specify shared library search path at runtime

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 11.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tallia1
    wrote on last edited by
    #1

    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.)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tallia1
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tallia1
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            [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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tallia1
                wrote on last edited by
                #7

                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".

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved