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. Trouble with shared object
Servers for Qt installer are currently down

Trouble with shared object

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 5.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.
  • D Offline
    D Offline
    dorik
    wrote on 4 Mar 2011, 04:43 last edited by
    #1

    Hello,

    I am trying to dynamically load a shared object into my application. Both the application and the shared object are compiled with Qt. The shared object contains one class- a subclass of a class defined in the application (which is itself a subclass of QObject). I am exporting symbols in the application by adding -Wl,export-dynamic to the compile flags so that symbols in the shared object will be resolved when it's loaded in. Whether I use QLibrary or dlopen, I am always getting the following error when loading it.

    undefined symbol: _ZNK3Orb9Component9Component10metaObjectEv

    If I put the Q_OBJECT macro in the subclass (which I don't really want), a moc object is also created and linked into the library, and I get the following error when loading it:

    undefined symbol: _ZN3Orb9Component9Component16staticMetaObjectE

    Note: Component is the name of the parent class.

    Thanks for your help!

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on 4 Mar 2011, 06:57 last edited by
      #2

      The parent class isn't moc'ed.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dorik
        wrote on 4 Mar 2011, 07:05 last edited by
        #3

        I think it is. A moc object is definitely being created. Another thing to note is that I can get it to work if I include the base class definition in the shared library. But, I don't think I should have to do this.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on 4 Mar 2011, 09:03 last edited by
          #4

          Did I understand correct:

          you have an executable (the app) that vtries to export classes, that should be used by other binaries (exes, dynamic libraries)?

          That is not possible!

          You can export stuff from dynamic libraries and use them in executables, but not the other way round.

          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
          • D Offline
            D Offline
            dorik
            wrote on 4 Mar 2011, 09:44 last edited by
            #5

            That's what I originally thought. But apparently, a shared library can have unresolved symbols that the application can fill in. You can check here: "http://www.informit.com/articles/article.aspx?p=22435&rll=1":http://www.informit.com/articles/article.aspx?p=22435&rll=1

            I've made a simple example program before where the shared object contains a subclass of an object in the main app. The shared object does not contain the parent class's definition (only the header), but is resolved when loaded into the main app.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on 4 Mar 2011, 10:25 last edited by
              #6

              Ok, perhaps that works on Linux, but it does definitely not work on windows.

              But you would go the safe way, if you put the base class to a dll.

              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
              • D Offline
                D Offline
                dorik
                wrote on 4 Mar 2011, 10:35 last edited by
                #7

                That is an option. I could try putting the base class in it's own shared object. But, the base class has a static member. If it's linked in to the main application and the shared object, will there be two copies of of the member?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on 4 Mar 2011, 12:06 last edited by
                  #8

                  please define shared object...

                  If you mean a dll, then the answer is definitely no, as the dll is only loaded once. IUt doesn't matter if it's a windows dll or a Linux shared library (.so).

                  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
                  • D Offline
                    D Offline
                    dorik
                    wrote on 4 Mar 2011, 12:13 last edited by
                    #9

                    Oh right, I guess that's what shared objects are for! What if the base class was not put into its own shared library, but the definition cpp was put in both the shared library and the main app. Would that cause two static members?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on 4 Mar 2011, 12:18 last edited by
                      #10

                      YES.

                      That would create double defined methods and not really 2 static members. On windows, it would have 2 members, on linux only one. The last one wins. If one is of type int and one of type string, the last loaded one is the existing one. Same appears to initialization.

                      If the member is called static ... inside the cpp file, then it will appear twice, it it's a static member in the class, it will appear once on Linux, twice on windows.

                      But definitely not what you expect :-(

                      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
                      • D Offline
                        D Offline
                        dorik
                        wrote on 4 Mar 2011, 12:24 last edited by
                        #11

                        I see, thanks for clearing that up!

                        I would still really like to get it working where the shared object only has the subclass definition. The idea is that there will be many shared objects to work like plugins. They return a pointer to the base class and the main app uses it through polymorphism. I know this is possible but something just isn't working with Qt. I'm quite sure it has to do with moc objects. From what I can see, the base class moc is compiled in with the main app and the subclass moc is compiled into the .so. Why are there undefined symbols?

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dorik
                          wrote on 8 Mar 2011, 01:18 last edited by
                          #12

                          Interesting. After compiling the project with and without Wl,--export-dynamic, I checked the output of nm on the executable. I used nm -D to show the dynamic symbols and the output was the same in both cases. So does this mean Wl,--export-dynamic is not doing anything?

                          I've been including it under QMAKE_CXXFLAGS in my .pro file.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dorik
                            wrote on 8 Mar 2011, 01:27 last edited by
                            #13

                            I found the problem!! :)

                            So, Qt was including -Wl,--export-dynamic when it compiled each c++ file, but not in the final link. I added it in manually and things seem to work as I had hoped.

                            Is there another variable I can use in the .pro file?

                            1 Reply Last reply
                            0

                            1/13

                            4 Mar 2011, 04:43

                            • Login

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