Answer the question
In order to leave comments, you need to log in
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();
}
#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
#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
LibraryClass::LibraryClass() {
m_val = 0;
}
//метод объектовый
void LibraryClass::objectMethod() {
std::cout << "Внутреобъектовый метод библиотеки" << std::endl;
}
extern "C" TESTLIBRARY_EXPORT LibraryClass* getObject() {
return new LibraryClass();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question