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. how to load a dynamic library on demand from a QT method
Forum Updated to NodeBB v4.3 + New Features

how to load a dynamic library on demand from a QT method

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 10.4k Views 3 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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on last edited by
    #11

    Sure, but if you only call a function once, you don't need to save the pointer, just use it directly, say like this:

    ((void (*))(Tcl_Interp *) QLibrary("/path/to/libmy.so").resolve("My_Init"))(&my_tcl_struct);
    

    Hope I got all the parentheses right :-)

    kshegunovK 1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Qt Enthusiast
      wrote on last edited by
      #12

      after that can I call QT code dirrectlt

      QTreewidget* tree = new QTreeWidget()..

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #13

        I do not need to link the QT library in my QExecutable

        kshegunovK 1 Reply Last reply
        0
        • hskoglundH hskoglund

          Sure, but if you only call a function once, you don't need to save the pointer, just use it directly, say like this:

          ((void (*))(Tcl_Interp *) QLibrary("/path/to/libmy.so").resolve("My_Init"))(&my_tcl_struct);
          

          Hope I got all the parentheses right :-)

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #14

          @hskoglund
          That's why I usually typedef the type locally, looks simpler (and since I despise auto I'm a happy man not using it):

          typedef void (*MyInitPtr)(Tcl_Interp *);
          MyInitPtr My_Init = reinterpret_cast<MyInitPtr>( QLibrary("/path/to/libmy.so").resolve("My_Init") );
          if (My_Init)
              My_Init(&my_tcl_struct);
          

          @Qt-Enthusiast

          after that can I call QT code dirrectlt

          No, and you won't be able to accomplish that with QLibrary, it's a bit more involved than resolving a simple C-linkage function.

          Read and abide by the Qt Code of Conduct

          Q 1 Reply Last reply
          1
          • kshegunovK kshegunov

            @hskoglund
            That's why I usually typedef the type locally, looks simpler (and since I despise auto I'm a happy man not using it):

            typedef void (*MyInitPtr)(Tcl_Interp *);
            MyInitPtr My_Init = reinterpret_cast<MyInitPtr>( QLibrary("/path/to/libmy.so").resolve("My_Init") );
            if (My_Init)
                My_Init(&my_tcl_struct);
            

            @Qt-Enthusiast

            after that can I call QT code dirrectlt

            No, and you won't be able to accomplish that with QLibrary, it's a bit more involved than resolving a simple C-linkage function.

            Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #15

            Two questions

            1 ) Do not need to link the QT library in my final Executable
            2) also Can u write a sample application how will multiple calls to many Qt functions codes can be called after

            typedef void (*MyInitPtr)(Tcl_Interp *);
            MyInitPtr My_Init = reinterpret_cast<MyInitPtr>( QLibrary("/path/to/libmy.so").resolve("My_Init") );
            if (My_Init)
            My_Init(&my_tcl_struct);

            1 Reply Last reply
            0
            • Q Qt Enthusiast

              I do not need to link the QT library in my QExecutable

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #16

              @Qt-Enthusiast

              Okay, @mrjj encouraged me to give you the full reasoning why those shenanigans are bad. Hence, I'm going to do so. Suppose you have a class MyClass with declaration in "myclass.h":

              class MyClass
              {
              public:
                  MyClass();
                  ~MyClass();
              
                  void myMethod(int);
              }
              

              and you want to call myMethod(int) of a newly created object of that class. But also suppose that class is compiled into a dynamic library myclasslib.so for all intents and purposes of this discussion. Suppose that the fully decorated symbols' names for the methods of this class are as follows:

              • MyClass::MyClass$constrDecoration for MyClass::MyClass
              • MyClass::MyClass$destrDecoration for MyClass::~MyClass
              • MyClass::MyClass$methodDecoration for MyClass::myMethod(int)

              So this is how you can create an object, call it's constructor, call the method and finally call the destructor and free the allocated memory (code is not tested, but should be working in principle):

              #include "myclass.h"
              
              int main()
              {
                  MyClass * obj = reinterpret_cast<MyClass *>(::malloc(sizeof(MyClass))); // Allocate the object
                  // Define two types for a pointer to method with and without an argument
                  typedef void (MyClass::*MyMethodNoArgPtr)();
                  typedef void (MyClass::*MyMethodIntArgPtr)(int);
              
                  // Get the library
                  QLibrary myLib("myclasslib.so"); //< This is the binary, in which the code for `MyClass` is compiled
                  // Resolve the constructor, destructor and myMethod (notice the fully decorated symbols' names)
                  MyMethodNoArgPtr _construct = reinterpret_cast<MyMethodNoArgPtr>(myLib.resolve("MyClass::MyClass$constrDecoration"));
                  MyMethodNoArgPtr _destruct = reinterpret_cast<MyMethodNoArgPtr>(myLib.resolve("MyClass::MyClass$destrDecoration"));
                  MyMethodIntArgPtr myMethod  = reinterpret_cast<MyMethodIntArgPtr>(myLib.resolve("MyClass::MyClass$methodDecoration"));
              
                  // Wow, now we can actually call what we need
                  (obj->*_construct)(); //< Call the constructor
                  (obj->*myMethod)(10); //< Call MyClass::myMethod(10)
                  (obj->*_destruct)()   //< Call the destructor
              
                  // We can free the memory now
                  ::free(obj);
                  return 0;
              }
              

              This all would be equivalent to:

              #include "myclass.h"
              
              int main()
              {
                  MyClass * obj = new MyClass();
                  obj->myMethod(10);
                  delete obj;
              
                  return 0;
              }
              

              if we were to leave the loader to do what it's supposed to do!

              Oh, and by the way, this gets even more complicated when the objects have virtual tables (which QObject derived classes do).

              Two questions

              1. Do not need to link the QT library in my final Executable

              If you use Qt, you do.

              1. also Can u write a sample application how will multiple calls to many Qt functions codes can be called after

              I don't understand the question. What multiple calls to which many Qt functions do you have in mind?

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #17

                because it application as big appilcation of Qt code

                kshegunovK 1 Reply Last reply
                0
                • Q Qt Enthusiast

                  because it application as big appilcation of Qt code

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #18

                  @Qt-Enthusiast
                  I don't follow.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by kshegunov
                    #19

                    for example

                    I have GUI application which has 1 lacs of QT code

                    for example

                       QTreeWidget *w  = new QTreeWidget;
                      QLabel * w = new QLablep
                    
                        class myTreeView:public QTreView {
                         Q_Object
                       };
                    

                    My question is If my load the dynamic library like

                    is there direct way algorithm is

                    void loadSharedObject() {  
                        //load the
                        // Call the function names directly
                        // no extra code for resolving the function /symbols names   
                        // for example  
                         fun1()
                         fun2()
                     }
                    
                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #20

                      My question is If my load the dynamic library like

                      is there direct way algorithm is

                      void loadSharedObject() {
                      //load the
                      // Call the function names directly
                      // no extra code for resolving the function /symbols names
                      // for example
                      fun1()
                      fun2()
                      }

                      because the dyanamic libary in my is QLibrary

                      kshegunovK 1 Reply Last reply
                      0
                      • Q Qt Enthusiast

                        My question is If my load the dynamic library like

                        is there direct way algorithm is

                        void loadSharedObject() {
                        //load the
                        // Call the function names directly
                        // no extra code for resolving the function /symbols names
                        // for example
                        fun1()
                        fun2()
                        }

                        because the dyanamic libary in my is QLibrary

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #21

                        @Qt-Enthusiast said in how to load a dynamic library on demand from a QT method:

                        // Call the function names directly
                        // no extra code for resolving the function /symbols names
                        // for example

                        No there isn't a way to do that. This is what linkers and loaders were created for. You can't leave a bunch of unresolved symbols that are supposed to be explicitly loaded at runtime, it just doesn't work that way.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        2

                        • Login

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