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. Is there any way to load a library dynamically?

Is there any way to load a library dynamically?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.2k 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.
  • S Offline
    S Offline
    stuartx
    wrote on last edited by
    #1

    Hi, I use Qt 4.7 on a Mac. I created several custom widgets. These widgets are compiled into a .dylib file installed in designer's plugin directory. When I open the designer in the Qt Creator, the .dylib is loaded. So I can use my widgets in my .ui file without any problem. Now I want to create different versions of the widgets/.dylib for different projects. And I want to load the right version of the .dylib for a specific project. Is there any way to achieve this?

    Thanks!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      If you mean loading DLLs at runtime, you need to use QLibrary.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1376111760"]If you mean loading DLLs at runtime, you need to use QLibrary.[/quote]

        AFAIK, this only works with "plain C" functions, but not with C++ objects. I never managed to load C++ objects dynamically (at runtime) from a DLL. It requires linking against the DLL via import library (.LIB file), I think.

        One workaround is to export some wrapper functions from the DLL:
        @#include "MyClass.h"

        extern "C"
        {
        __declspec(dllexport) MyClass* __cdecl newObject(void)
        {
        return new MyClass();
        }
        __declspec(dllexport) int __cdecl useObject(MyClass *o)
        {
        return o->doSomethingInteresting();
        }
        __declspec(dllexport) void __cdecl delObject(MyClass *o)
        {
        delete o;
        }
        }@

        Then use like this in the EXE file:
        @class MyClass;

        extern "C"
        {
        typedef MyClass* (*NewObjetFunc)(void);
        typedef int (UseObjetFunc)(MyClass);
        typedef void (DelObjetFunc)(MyClass);
        }

        NewObjectFunc newObjectPtr = NULL;
        UseObjectFunc useObjectPtr = NULL;
        DelObjectFunc delObjectPtr = NULL;

        QLibrary myLib("MyLibrary.dll")
        if(myLib.load())
        {
        newObjectPtr = (NewObjectFunc) myLib.resolve("newObject");
        useObjectPtr = (UseObjectFunc) myLib.resolve("useObject");
        delObjectPtr = (DelObjectFunc) myLib.resolve("delObject");
        }

        if(newObjectPtr && useObjectPtr && delObjectPtr)
        {
        MyClass *obj = newObjectPtr();

        int result = useObjectPtr(obj); //Actually use object here!
        
        delObjectPtr(obj);
        obj = NULL;
        

        }@

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          QLibrary is - as MuldeR said - for C functions resolving.
          You can use QPlugInLoader for loading C++ functions, but only when they are using "Qt plugin mechanismns":http://qt-project.org/doc/qt-5.0/qtcore/plugins-howto.html. Or link to them dynamically.

          Please note that for C++ symbol resolving the various things (e.g., compiler version, build mode, etc.) need to match in order to load a plugin successfully.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stuartx
            wrote on last edited by
            #5

            Thanks for all the help. I will try to see which method is proper to my case.

            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