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.3k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 2 Sept 2016, 11:20 last edited by
    #4

    if I multiple calls of QT functions then do I need to resolve each an every call for exxample I have 1 lac calls to Qt functions then do I need to resolve each an every function

    M 1 Reply Last reply 2 Sept 2016, 11:26
    0
    • Q Qt Enthusiast
      2 Sept 2016, 11:20

      if I multiple calls of QT functions then do I need to resolve each an every call for exxample I have 1 lac calls to Qt functions then do I need to resolve each an every function

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Sept 2016, 11:26 last edited by
      #5

      @Qt-Enthusiast
      Hi
      I think yes. for your setup.
      Normally one can use plugins and interfaces and it removes the resolve part
      but you are using plain shared lib, so not not sure what u need or dont need :)

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on 2 Sept 2016, 11:54 last edited by
        #6

        Can u guide how

        Normally one can use plugins and interfaces and it removes the resolve part
        but you are using plain shared lib, so not not sure what u need or dont nee

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on 2 Sept 2016, 11:57 last edited by
          #7

          What is use of QLibrary::ResolveAllSymbolsHint

          R 1 Reply Last reply 2 Sept 2016, 12:06
          0
          • Q Qt Enthusiast
            2 Sept 2016, 11:57

            What is use of QLibrary::ResolveAllSymbolsHint

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 2 Sept 2016, 12:06 last edited by
            #8

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

            What is use of QLibrary::ResolveAllSymbolsHint

            this just resolves all symbols at load time, as said in the docs.
            But still you need a pointer to the function you want to call.

            --- 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
            • H Online
              H Online
              hskoglund
              wrote on 2 Sept 2016, 12:15 last edited by
              #9

              Hi, you can use a function pointer like this:

              auto f = (void (*))(Tcl_Interp *) QLibrary("/path/to/libmy.so").resolve("My_Init");
              
              f(&my_tcl_struct);     // calls your function
              
              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on 2 Sept 2016, 12:28 last edited by
                #10

                for each Call to Qt function I need to have a function pointer correct ?

                1 Reply Last reply
                0
                • H Online
                  H Online
                  hskoglund
                  wrote on 2 Sept 2016, 12:40 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 :-)

                  K 1 Reply Last reply 2 Sept 2016, 12:52
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on 2 Sept 2016, 12:48 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 2 Sept 2016, 12:50 last edited by
                      #13

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

                      K 1 Reply Last reply 2 Sept 2016, 13:40
                      0
                      • H hskoglund
                        2 Sept 2016, 12:40

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

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 2 Sept 2016, 12:52 last edited by kshegunov 9 Feb 2016, 12:55
                        #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 2 Sept 2016, 13:27
                        1
                        • K kshegunov
                          2 Sept 2016, 12:52

                          @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 2 Sept 2016, 13:27 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
                            2 Sept 2016, 12:50

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

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 2 Sept 2016, 13:40 last edited by kshegunov 9 Feb 2016, 13:55
                            #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 3 Sept 2016, 09:56 last edited by
                              #17

                              because it application as big appilcation of Qt code

                              K 1 Reply Last reply 3 Sept 2016, 13:52
                              0
                              • Q Qt Enthusiast
                                3 Sept 2016, 09:56

                                because it application as big appilcation of Qt code

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 3 Sept 2016, 13:52 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 4 Sept 2016, 20:46 last edited by kshegunov 9 Apr 2016, 20:56
                                  #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 4 Sept 2016, 20:55 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

                                    K 1 Reply Last reply 4 Sept 2016, 21:18
                                    0
                                    • Q Qt Enthusiast
                                      4 Sept 2016, 20:55

                                      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

                                      K Offline
                                      K Offline
                                      kshegunov
                                      Moderators
                                      wrote on 4 Sept 2016, 21:18 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

                                      13/21

                                      2 Sept 2016, 12:50

                                      • Login

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