QLibrary - import a class

I have a QT library and I want to import it into another project.

Now, since I want this, even when I change the library, another project does not need to be compiled again, I started using QLibrary.

But ... I can not import the class. Or better, I can import the class, but I cannot access its methods.

This is an example I made.

This is a class declaration:

class TESTDLL_LIBSHARED_EXPORT TestDLL_lib
{

public:
    TestDLL_lib();

    int a;
    int b;
    int c;

    int getValues();
}; 

and this is the implementation:

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

while this is the main file, in another 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 I can access all of the data values ( a, b, c) of the object myClassInstance(and if I replaced them in the DLL, they will also be changed in the program without a rebuild), but I can not call myClassInstance->getValues()because getting

main.obj:-1: error: LNK2001: unresolved external symbol "__declspec(dllimport) public: int __thiscall TestDLL_lib::getValues(void)" (__imp_?getValues@TestDLL_lib@@QAEHXZ)

How can i solve this? Is it possible to call methods from imported classes?

Thank..

+4
1

, . , , ( ). , vtable:

virtual , ( ). , , virtual (, ). TestDLL_lib , . TestDLL_lib , vtable , "" TestDLL_lib vtable.

dore virtual virtual dtor. , delete .

, , "" . , . , , ( ..). . , 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();
}
+5

All Articles