Results 1 to 6 of 6

Thread: Must include not only h files but cpp files when load a DLL using QPluginLoader

  1. #1
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Must include not only h files but cpp files when load a DLL using QPluginLoader

    Hi there, I got some problem when using QPluginLoader.
    1) Firstly, I wrote the interface named “tcpPlunginInterface.h”. The content:

    Qt Code:
    1. #ifndef TCPPLUGININTERFACE_H
    2. #define TCPPLUGININTERFACE_H
    3.  
    4. #include "tcpclient_os.h"
    5.  
    6. class tcpPluginInterface
    7. {
    8. public:
    9.  
    10. virtual tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
    11. QObject *parent = 0) = 0;
    12. };
    13.  
    14. QT_BEGIN_NAMESPACE
    15. Q_DECLARE_INTERFACE(tcpPluginInterface,"ltcpOSDLL/1.0.0")
    16. QT_END_NAMESPACE
    17.  
    18. #endif // TCPPLUGININTERFACE_H
    To copy to clipboard, switch view to plain text mode 
    2) Secondly, I wrote a class called “ltcpOS”. The instance of this class contained a function “getPluginInst”, which would return a point type.
    “ltcpOS.h”:
    Qt Code:
    1. #ifndef LTCPOS_H
    2. #define LTCPOS_H
    3.  
    4. #include <QObject>
    5. #include <QtPlugin>
    6. #include "tcpPluginInterface.h"
    7.  
    8. class ltcpOS : public QObject,tcpPluginInterface
    9. {
    10. Q_OBJECT
    11. Q_INTERFACES(tcpPluginInterface)
    12. public:
    13. ltcpOS();
    14. ~ltcpOS();
    15.  
    16. tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
    17. QObject *parent = 0);
    18. //the “tcpClient_Os” is a self-defined class.
    19.  
    20. public:
    21. tcpClient_Os *os;
    22.  
    23. signals:
    24.  
    25. public slots:
    26.  
    27. };
    28.  
    29. #endif // LTCPOS_H
    To copy to clipboard, switch view to plain text mode 
    “ltcpos.cpp”:
    Qt Code:
    1. #include "ltcpos.h"
    2.  
    3. ltcpOS::ltcpOS()
    4. {
    5. }
    6.  
    7. ltcpOS::~ltcpOS()
    8. {
    9. os->deleteLater();
    10. }
    11.  
    12. Q_EXPORT_PLUGIN2(ltcpOS,ltcpOS)
    13.  
    14. tcpClient_Os *ltcpOS::getPluginInst(VhostInfo hostInfo, CtcpOpt *tcpOpt, QObject *parent)
    15. {
    16. os = new tcpClient_Os(hostInfo, tcpOpt);
    17. return os;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Then I compiled these file in release mode, and generated a “ltcpOS1.dll” DLL file. So I could load it and use it in other project.

    The problem I encountered is, when I use a QPluginLoader to load the DLL, QT doesn’t know what the return value is. This is my code which loads the DLL:
    Qt Code:
    1. QPluginLoader dllLoader("E:/QTProject/tcpClient3/ltcpOS1.dll");
    2.  
    3. QObject *tcpObj = dllLoader.instance();
    4.  
    5. if(!tcpObj)
    6. {
    7. qDebug() << dllLoader.errorString();
    8. return;
    9. }
    10.  
    11. tcpPluginInterface *os_inst = qobject_cast<tcpPluginInterface*>(tcpObj);
    12.  
    13. client = os_inst->getPluginInst(hostInfo, &ProtIOBuf);
    14.  
    15. client->run();
    To copy to clipboard, switch view to plain text mode 

    I have included the header file such like “tcpClient_os.h”, and add “QT += network” to the *.pro file, but I still get the compile errors:
    error:undefined reference to `tcpClient_Os::run()'
    error:undefined reference to `tcpClient_Os::slot_stop()'
    …..etc



    But When I add the all the cpp files (such like tcpClient_os.cpp), which contains the all implement part, to the *.pro. It’s OK. I don’t want to add those cpp files because that made my DLL useless. I’m going to be insane about that problem, it bother me more than a week…Please,help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Must include not only h files but cpp files when load a DLL using QPluginLoader

    If you include tcpClient_os.h then you also need to provide bodies of those methods. With plugins you should only include tcpPlunginInterface.h. Moreover on Windows you need to use Q_DECL_EXPORT in your plugin classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Must include not only h files but cpp files when load a DLL using QPluginLoader

    Thanks,wysota.
    I will give it a try to see how it works...

  4. #4
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Must include not only h files but cpp files when load a DLL using QPluginLoader

    Sorry,bro.
    I tried it in many ways,and seemingly it just doesn't work...

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,943
    Thanks
    276
    Thanked 804 Times in 793 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Must include not only h files but cpp files when load a DLL using QPluginLoader

    Qt Code:
    1. class ltcpOS : public QObject,tcpPluginInterface
    To copy to clipboard, switch view to plain text mode 

    I'm not sure I am remembering my C++ correctly, but I believe that this declaration is the same as:

    Qt Code:
    1. class ltcpOS : public QObject, private tcpPluginInterface
    To copy to clipboard, switch view to plain text mode 

    which you definitely do not want. It should be declared as:

    Qt Code:
    1. class ltcpOS : public QObject, public tcpPluginInterface
    To copy to clipboard, switch view to plain text mode 

    and you need to do the other things Wysota said.

  6. #6
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Must include not only h files but cpp files when load a DLL using QPluginLoader

    Sorry bro, it doesn't work...
    For now I am concerning about my understanding of DLL. It package the Methods not the Specific system source. In the project, I tried line by line to figure out where the problem occurred. Now I found the procedure of loading a DLL is correct. the problem shows up when I try to use pointer "client" to call its member function(i.e. client->run(); ).the DLL would only return a pointer and it didn't "know" what is tcpClient_OS, without CPP files, the compiler didn't "know" it too...I guess the reason made me failed probably like in that way..

Similar Threads

  1. Combining include files
    By marcvanriet in forum General Programming
    Replies: 12
    Last Post: 17th October 2010, 09:55
  2. How to include existing files to project?
    By Asperamanca in forum Qt Tools
    Replies: 2
    Last Post: 14th November 2009, 20:50
  3. Order of include files [c++ related]
    By Lykurg in forum Newbie
    Replies: 1
    Last Post: 10th November 2008, 12:56
  4. qmake: include files?
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 16th May 2007, 23:34
  5. problem with include files
    By JR in forum General Discussion
    Replies: 2
    Last Post: 22nd December 2006, 20:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.