Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLibrary not working
Forum Updated to NodeBB v4.3 + New Features

QLibrary not working

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 416 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1
    #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

    jsulmJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @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).

      I Offline
      I Offline
      IknowQT
      wrote on last edited by
      #5

      @jsulm

      There is something wrong with creating a dynamic library.

      extern "C" MYFIRST_LIB_EXPORT void TEST_FUNC_LIB_For()
      

      MYFIRST_LIB_EXPORT I added this and it worked fine.

      1 Reply Last reply
      0
      • I IknowQT
        #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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply
        3
        • jsulmJ jsulm

          @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

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @jsulm

           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?

          jsulmJ 1 Reply Last reply
          0
          • I IknowQT

            @jsulm

             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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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).

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            I 1 Reply Last reply
            0
            • jsulmJ jsulm

              @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).

              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              @jsulm

              There is something wrong with creating a dynamic library.

              extern "C" MYFIRST_LIB_EXPORT void TEST_FUNC_LIB_For()
              

              MYFIRST_LIB_EXPORT I added this and it worked fine.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved