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
Code:
class __declspec(dllexport) Funcs
{
public:
Funcs();
void Print();
};
#DllProg
Code:
#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
Code:
int main(int argc, char *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.
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.
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.
Re: Linking C++ Dll containing class in QT Application
have you read the link I posted?
It is explained
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.