Results 1 to 5 of 5

Thread: Linking C++ Dll containing class in QT Application

  1. #1
    Join Date
    May 2011
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Linking C++ Dll containing class in QT Application

    Hi,

    I am new to QT development. I have below query.

    I am writing a simple dll in C++ which has a class with member function.

    #DllProg.h
    Qt Code:
    1. class __declspec(dllexport) Funcs
    2. {
    3. public:
    4. Funcs();
    5. void Print();
    6. };
    To copy to clipboard, switch view to plain text mode 
    #DllProg

    Qt Code:
    1. #include "DllProg.h"
    2.  
    3. #include <stdexcept>
    4. #include <iostream>
    5. #include <string>
    6.  
    7. using namespace std;
    8.  
    9. __declspec(dllexport) Funcs::Funcs()
    10. {
    11. cout<<"Funcs : In Constructor"<<endl;
    12. }
    13.  
    14. __declspec(dllexport) void Funcs::Print()
    15. {
    16. cout<<"Print : In DLL";
    17. }
    18.  
    19. extern "C" __declspec(dllexport) Funcs* getObject()
    20. {
    21. cout << "In getObject() "<<endl;
    22. return new Funcs();
    23. }
    To copy to clipboard, switch view to plain text mode 

    #QT Application

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. typedef Funcs* (*pf)();
    6.  
    7. QLibrary myLib("D:\\Practice\\QT\\QTClassApp-build-desktop\\debug\\DllProg.dll");
    8. myLib.load();
    9.  
    10. if(myLib.isLoaded())
    11. {
    12. pf fptr = (pf)myLib.resolve("getObject");
    13. Funcs *s = fptr();
    14.  
    15. printf("getObject Returned - %p" , s);
    16. s->Print();
    17. }
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    Here I am getting Error on statement s->Print();
    getObject() is properly getting called and returning instance of the class. But if I put call to Print its giving Linker Error - undefined reference to `Funcs::Print()'

    #.pro
    win32: LIBS += -L$$PWD/../QTClassApp-build-desktop/debug/ -lDllProg
    INCLUDEPATH += $$PWD/../QTClassApp-build-desktop/debug
    DEPENDPATH += $$PWD/../QTClassApp-build-desktop/debug


    I found the ablove code on QT forum. I am not sure how correct is the code.

    Thanks in Advance.
    Last edited by high_flyer; 5th May 2011 at 13:07. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Linking C++ Dll containing class in QT Application

    The error is to use hardcoded __declspec(dllexport)

    When you import the library you have to pass __declspec(dllimport).

    Read here for information.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    May 2011
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Linking C++ Dll containing class in QT Application

    Thanks for the reply.

    Could you please explain where to use dllimport?

    I can successfully call the dll functions (non-members) from my QT application.

    But I want to call class members presents in dll.
    Last edited by ujwala; 6th May 2011 at 07:04.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Linking C++ Dll containing class in QT Application

    have you read the link I posted?

    It is explained
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    May 2011
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Linking C++ Dll containing class in QT Application

    Hi,
    I have gone thru the link you have provided. But I think that describes the behaviour of libraries created in QT. But I have C++ Library compiled in MSVS 2010.

    Correct me If m wrong.

    Thanks.

Similar Threads

  1. Replies: 10
    Last Post: 20th December 2015, 11:14
  2. Shared Object In Qt and Linking to main application
    By Ashutosh2k1 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2011, 10:36
  3. Linking error when using library in other application
    By JPNaude in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2010, 09:20
  4. Static Linking Application for Realease
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2007, 18:12
  5. Replies: 2
    Last Post: 1st August 2007, 15:04

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.