K
K
Kamikaze10242021-02-15 20:02:10
Qt
Kamikaze1024, 2021-02-15 20:02:10

How to load class object from dll library?

I have a library that I want to get a class object from, but I can't do it.

Library download code:

#include "../TestLibrary/TestLibrary.hpp"

int main(int argc, char *argv[]) {

#ifdef __WIN32__
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(CP_UTF8);
#endif

    QCoreApplication a(argc, argv);

    QLibrary testLibrary("../TestLibrary/libTestLibrary_13.so");
    if(!testLibrary.isLoaded()) {
        std::cout << "Не смог загрузить библиотеку" << std::endl;
        a.exit(0);
    }

    typedef LibraryClass* (*getObject)();
    getObject f = (getObject)testLibrary.resolve("getObject");
    if(!f) {
        std::cout << "Не смог получить getObject" << std::endl;
        a.exit(0);
    }
    LibraryClass* tlInstance = f();
    tlInstance->objectMethod();

    testLibrary.unload();

    return a.exec();
}


Global header with macros:
#ifndef TESTLIBRARY_GLOBAL_H
#define TESTLIBRARY_GLOBAL_H

#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#  define Q_DECL_EXPORT __declspec(dllexport)
#  define Q_DECL_IMPORT __declspec(dllimport)
#else
#  define Q_DECL_EXPORT     __attribute__((visibility("default")))
#  define Q_DECL_IMPORT     __attribute__((visibility("default")))
#endif

#if defined(TESTLIBRARY_LIBRARY)
#  define TESTLIBRARY_EXPORT Q_DECL_EXPORT
#else
#  define TESTLIBRARY_EXPORT Q_DECL_IMPORT
#endif


Library header code:
#ifndef LIBRARYCLASS_HPP
#define LIBRARYCLASS_HPP

#include "TestLibrary_global.h"

class TESTLIBRARY_EXPORT LibraryClass {
private:
    int m_val;

public:
    LibraryClass();
    void objectMethod();
};

#endif // LIBRARYCLASS_HPP


.cpp file:
LibraryClass::LibraryClass() {
    m_val = 0;
}

//метод объектовый
void LibraryClass::objectMethod() {
    std::cout << "Внутреобъектовый метод библиотеки" << std::endl;
}

extern "C" TESTLIBRARY_EXPORT LibraryClass* getObject() {
    return new LibraryClass();
}


When I try to build and execute the library loading code, I get undefined reference LibraryClass::LibraryClass().
What am I doing wrong, and what does it take to get an instance of the class at runtime?

gcc/mingw compiler.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
r_makis, 2021-02-16
@r_makis

not visible in main file
#include "LibraryClass.hpp"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question