我有一个QT库,我想在另一个项目中导入它。QLibrary - 进口一个类
现在,因为我想这样,即使当我修改库时,其他项目不需要再次编译,我开始使用QLibrary。
但是...我无法导入类。或者更好,我可以导入这个类,但是我不能访问它的方法。
这是我做的例子。
这是类声明:
class TESTDLL_LIBSHARED_EXPORT TestDLL_lib
{
public:
TestDLL_lib();
int a;
int b;
int c;
int getValues();
};
,这实现:
#include "testdll_lib.h"
TestDLL_lib::TestDLL_lib()
{
a = 10;
b = 20;
c = 30;
}
int TestDLL_lib::getValues()
{
return a+b+c;
}
extern "C" TESTDLL_LIBSHARED_EXPORT TestDLL_lib* create_TestDLL_lib()
{
return new TestDLL_lib();
}
,而这是主要的文件,在其他项目:
#include <testdll_lib.h>
#include <QDebug>
#include <QLibrary>
int main(int argc, char *argv[])
{
QLibrary library("TestDLL_lib");
if (library.load())
{
typedef TestDLL_lib* (*create_TestDLL_lib_fun)();
create_TestDLL_lib_fun create_TestDLL_lib = (create_TestDLL_lib_fun)library.resolve("create_TestDLL_lib");
if (create_TestDLL_lib)
{
TestDLL_lib *myClassInstance = create_TestDLL_lib();
if (myClassInstance)
{
//qDebug() << QString::number(myClassInstance->getValues());
qDebug() << QString::number(myClassInstance->a) + " " + QString::number(myClassInstance->b) + " " + QString::number(myClassInstance->c);
}
}
library.unload();
}
}
现在,我可以访问所有的数据值(a
,b
,c
)对象myClassInstance
(如果我在DLL中改变他们,他们也得到程序中的改变不会重建),但我不能叫myClassInstance->getValues()
,因为我得到
main.obj:-1: error: LNK2001: unresolved external symbol "__declspec(dllimport) public: int __thiscall TestDLL_lib::getValues(void)" ([email protected][email protected]@QAEHXZ)
我怎样才能解决这个问题的?是否可以从导入的类中调用方法?
谢谢..
谢谢!有用! :) – frarugi87 2014-10-07 12:44:35
另请参见:[使用插件扩展Qt应用程序](http://qt-project.org/doc/qt-5/plugins-howto.html#the-lower-level-api-extending-t-t-t-applications) – dom0 2014-10-07 15:46:57