PDA

View Full Version : Linking C++ Dll containing class in QT Application



ujwala
5th May 2011, 11:38
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

class __declspec(dllexport) Funcs
{
public:
Funcs();
void Print();
};


#DllProg


#include "DllProg.h"

#include <stdexcept>
#include <iostream>
#include <string>

using namespace std;

__declspec(dllexport) Funcs::Funcs()
{
cout<<"Funcs : In Constructor"<<endl;
}

__declspec(dllexport) void Funcs::Print()
{
cout<<"Print : In DLL";
}

extern "C" __declspec(dllexport) Funcs* getObject()
{
cout << "In getObject() "<<endl;
return new Funcs();
}


#QT Application


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

typedef Funcs* (*pf)();

QLibrary myLib("D:\\Practice\\QT\\QTClassApp-build-desktop\\debug\\DllProg.dll");
myLib.load();

if(myLib.isLoaded())
{
pf fptr = (pf)myLib.resolve("getObject");
Funcs *s = fptr();

printf("getObject Returned - %p" , s);
s->Print();
}
return a.exec();
}

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.

mcosta
5th May 2011, 13:33
The error is to use hardcoded __declspec(dllexport)

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

Read here for information.

ujwala
6th May 2011, 06:59
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.

mcosta
6th May 2011, 07:39
have you read the link I posted?

It is explained

ujwala
9th May 2011, 08:32
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.