C++ QLibrary-导入一个类
我有一个QT库,我想在另一个项目中导入它 现在,由于我希望这样,即使在修改库时,也不需要再次编译另一个项目,因此我开始使用QLibrary 但是。。。我无法导入类。或者更好,我可以导入该类,但无法访问其方法 这就是我举的例子 这是类声明:C++ QLibrary-导入一个类,c++,qt,qt5,qlibrary,C++,Qt,Qt5,Qlibrary,我有一个QT库,我想在另一个项目中导入它 现在,由于我希望这样,即使在修改库时,也不需要再次编译另一个项目,因此我开始使用QLibrary 但是。。。我无法导入类。或者更好,我可以导入该类,但无法访问其方法 这就是我举的例子 这是类声明: class TESTDLL_LIBSHARED_EXPORT TestDLL_lib { public: TestDLL_lib(); int a; int b; int c; int getValues(); }
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();
}
}
我怎样才能解决这个问题?可以从导入的类调用方法吗
谢谢..您不能对运行时导入的类调用方法。这是因为编译器在编译时链接这些调用,而不是在运行时(它不能这样做)。我们的好朋友vtable提供了一条出路: 您可以在实现接口的类上调用
virtual
方法(该接口在运行时不是“导入的”)。这意味着使用virtual
(可能是纯虚拟)方法定义一个类来定义接口<然后,code>TestDLL_lib将继承该接口,实现这些方法。您可以通过该接口引用TestDLL_-lib
实例,并通过该接口调用方法,有效地通过接口的vtable调用它们,该vtable被TestDLL_-lib
s vtable“取代”
别忘了让你的任务虚拟
,并在界面上添加一个虚拟
dtor。如果不这样做,则无法通过接口指针安全地删除实例
我还可以解释为什么您可以访问成员,但不能对“导入的”类调用函数。成员由内存位置访问,而内存位置仅由编译器定义。因此,编译器生成代码来访问成员,而无需引用任何类的符号(方法等)。这反过来又导致没有链接依赖性。但是请注意,如果更改类(例如添加或删除成员),则需要使用DLL重新编译DLL和应用程序,因为这会更改内存布局
class TestInterface
{
public:
virtual ~TestInterface()
{
}
virtual int getValues() = 0;
}
class TESTDLL_LIBSHARED_EXPORT TestDLL_lib : public TestInterface
{
public:
TestDLL_lib();
virtual ~TestDLL_lib();
int a;
int b;
int c;
int getValues() override; // MSVC may not support "override"
};
// return pointer to interface!
// TestDLL_lib can and should be completely hidden from the application
extern "C" TESTDLL_LIBSHARED_EXPORT TestInterface *create_TestDLL_lib()
{
return new TestDLL_lib();
}
另见:
class TestInterface
{
public:
virtual ~TestInterface()
{
}
virtual int getValues() = 0;
}
class TESTDLL_LIBSHARED_EXPORT TestDLL_lib : public TestInterface
{
public:
TestDLL_lib();
virtual ~TestDLL_lib();
int a;
int b;
int c;
int getValues() override; // MSVC may not support "override"
};
// return pointer to interface!
// TestDLL_lib can and should be completely hidden from the application
extern "C" TESTDLL_LIBSHARED_EXPORT TestInterface *create_TestDLL_lib()
{
return new TestDLL_lib();
}