我有一個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