QLibrary not working
Solved
General and Desktop
-
#ifndef MYFIRST_LIB_H #define MYFIRST_LIB_H #include "Myfirst_Lib_global.h" #include "QDebug" extern "C" void TEST_FUNC_LIB(); extern "C" void TEST_FUNC_LIB_For() { qDebug() << "Show Myfirst_Lib"; for(int i=0; i<10; i++) { qDebug() << QString("Index: 1%\n").arg(i); } } class MYFIRST_LIB_EXPORT Myfirst_Lib { public: Myfirst_Lib(); void TEST_FUNC(); }; #endif // MYFIRST_LIB_H #include "myfirst_lib.h" Myfirst_Lib::Myfirst_Lib() { qDebug() << "Show Myfirst_Lib"; } void Myfirst_Lib::TEST_FUNC() { qDebug() << "Show Myfirst_Lib"; for(int i=0; i<10; i++) { qDebug() << QString("Index: ").arg(i); } } void TEST_FUNC_LIB() { qDebug() << "Show Myfirst_Lib"; for(int i=0; i<10; i++) { qDebug() << QString("Index: 1%\n").arg(i); } } #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QLibrary_Test(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::QLibrary_Test() { qDebug() << "QLibrary_Test\n"; QLibrary g_testLibrary; g_testLibrary.setFileName("C:/Users/yhJeong/Documents/QT_Proj/Lib_TestProj/Depoly_Exec/debug/Myfirst_Lib.dll"); if(g_testLibrary.isLoaded() == true) g_testLibrary.unload(); if((g_testLibrary.load()) == false) qDebug() << "Test Library Load Error\n"; typedef void (*p_libFunc)(void); p_libFunc libraryExe = NULL; libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC_LIB"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("Myfirst_Lib"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC_LIB_For"); if(libraryExe) libraryExe(); }
I tried to load the library, but it didn't work. I succeeded in importing the dll file in that location, but libraryExe is 0. Please tell me what's wrong
-
@IknowQT said in QLibrary not working:
Please tell me what's wrong
You did not export TEST_FUNC_LIB() function.
See https://doc.qt.io/qt-5/qlibrary.html#resolve -
libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC_LIB"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("Myfirst_Lib"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC"); if(libraryExe) libraryExe(); libraryExe = (p_libFunc)g_testLibrary.resolve("TEST_FUNC_LIB_For"); if(libraryExe) libraryExe();
Calls the dll function with resolve, but returns false in the conditional statement.
I don't understand why it returns false. Where did the problem come from? -
@IknowQT Which of the 4 functions?
Which conditional statement fails?
resolve() does not call any function.
Did you read what I wrote before and act accordingly (as reminder: on Windows you have to export symbols in a lib which you want to use outside of the lib).