c++ - QLibrary - import a class -



c++ - QLibrary - import a class -

i have qt library , want import in project.

now, since want that, when modify library, other project not need compiled again, started using qlibrary.

but... can't import class. or better, can import class, can't access methods.

this illustration made.

this class declaration:

class testdll_libshared_export testdll_lib { public: testdll_lib(); int a; int b; int c; int getvalues(); };

and implementation:

#include "testdll_lib.h" testdll_lib::testdll_lib() { = 10; b = 20; c = 30; } int testdll_lib::getvalues() { homecoming a+b+c; } extern "c" testdll_libshared_export testdll_lib* create_testdll_lib() { homecoming new testdll_lib(); }

while main file, in other project:

#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(); } }

now, can access info values (a, b, c) of object myclassinstance (and, if alter them in dll, changed in programme without rebuild) can't phone call myclassinstance->getvalues() because get

main.obj:-1: error: lnk2001: unresolved external symbol "__declspec(dllimport) public: int __thiscall testdll_lib::getvalues(void)" (__imp_?getvalues@testdll_lib@@qaehxz)

how can solve this? possible phone call methods imported classes?

thank you..

you cannot phone call methods on classes imported @ runtime. because compiler links these calls @ compile-time , not @ run-time (which cannot do). way out provided our ol' friend, vtable:

you can, phone call virtual methods on classes implementing interface (the interface not "imported" @ runtime). means define class defining interface using virtual (possibly pure virtual) methods. testdll_lib inherit interface, implementing methods. refer testdll_lib instance via interface , phone call methods trough interface, calling them trough vtable of interface, "superseded" testdll_libs vtable.

don't forget create d'tor virtual , add together virtual dtor interface. if don't cannot safely delete instance trough interface pointer.

i might explain why can access members, not phone call functions on "imported" classes. members accessed memory location, , memory location solely defined compiler. compiler generates code access members without ever referring of classes' symbols (methods , on). in turns leads no linkage dependency. note need recompile both dll , application using dll if alter class, e.g. adding or removing member, since changes memory layout.

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 back upwards "override" }; // homecoming pointer interface! // testdll_lib can , should hidden application extern "c" testdll_libshared_export testinterface *create_testdll_lib() { homecoming new testdll_lib(); }

c++ qt qt5 qlibrary

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -