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. Running Qt Application from .dll
Forum Updated to NodeBB v4.3 + New Features

Running Qt Application from .dll

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 4.6k Views 3 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #7

    What exact errors are you getting ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    T 1 Reply Last reply
    0
    • SGaistS SGaist

      What exact errors are you getting ?

      T Offline
      T Offline
      Theok
      wrote on last edited by
      #8

      @SGaist ,
      thanks for replying,
      I'm starting a new project (deleting the mainwindow.cpp,mainwindow.ui,,mainwindow.h so to include my main window created from the dll).
      my warnings are:

      moc_dll.cpp:58: warning: C4273: 'MainWindow::qt_static_metacall': inconsistent dll linkage , multiple times and the error:
      moc_dll.cpp:65: error: C2491: 'MainWindow::staticMetaObject': definition of dllimport static data member not allowed

      The dll code is:
      dll.h :

      #ifndef DLL_H
      #define DLL_H

      #include "dll_global.h"
      #include <QMainWindow>

      class DLLSHARED_EXPORT DLL
      {

      public:
      DLL();
      };

      namespace Ui {
      class MainWindow;
      }

      class DLLSHARED_EXPORT MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = nullptr);
      ~MainWindow();

      private:
      Ui::MainWindow *ui;
      };

      #endif // DLL_H**

      dll.cpp

      DLL::DLL()
      {
      }

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      but the errrors come from the moc_dll.cpp...
      In the beginning of the moc_dll.cpp file i have this comments:

      #if !defined(Q_MOC_OUTPUT_REVISION)
      #error "The header file 'dll.h' doesn't include <QObject>."
      #elif Q_MOC_OUTPUT_REVISION != 67
      #error "This file was generated using the moc from 5.12.0. It"
      #error "cannot be used with the include files from this version of Qt."
      #error "(The moc has changed too much.)"

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Theok
        wrote on last edited by
        #9

        Forgot the includes in the dll.cpp:

        #include "dll.h"
        #include "ui_mainwindow.h"

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #10

          If I may, you should avoid such naming, this can make reading errors and warnings confusing. Give your dll a proper name even if it's foobar so it can be clearly identified with regards to the errors and related stuff. Same goes for the files you are using.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • T Offline
            T Offline
            Theok
            wrote on last edited by
            #11

            @SGaist
            Thanks for the advice, i changed the Mainwindow to mywidget and the name of the library to mylibrary. it seems more clear code now but still have the same errors in the moc_ file:

            1)moc_mylibrary.cpp:58: warning: C4273: 'mywidget::qt_static_metacall': inconsistent dll linkage
            2)error: C2491: 'mywidget::staticMetaObject': definition of dllimport static data member not allowed

            Is it a problem that i create in QT a C++ Library? Do i need to make a QT Creator Plugin to load these type of QT objects in my new Qapplication? Maybe these errors indicate that i am in the wrong way?

            Here is the code of the moc_mylibrary file where the errors exists:

            **#include "../../WidgetAppforLibrary/mylibrary.h"
            #include <QtCore/qbytearray.h>
            #include <QtCore/qmetatype.h>
            #if !defined(Q_MOC_OUTPUT_REVISION)
            #error "The header file 'mylibrary.h' doesn't include <QObject>."
            #elif Q_MOC_OUTPUT_REVISION != 67
            #error "This file was generated using the moc from 5.12.1. It"
            #error "cannot be used with the include files from this version of Qt."
            #error "(The moc has changed too much.)"
            #endif

            QT_BEGIN_MOC_NAMESPACE
            QT_WARNING_PUSH
            QT_WARNING_DISABLE_DEPRECATED
            struct qt_meta_stringdata_mywidget_t {
            QByteArrayData data[1];
            char stringdata0[9];
            };
            #define QT_MOC_LITERAL(idx, ofs, len)
            Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len,
            qptrdiff(offsetof(qt_meta_stringdata_mywidget_t, stringdata0) + ofs
            - idx * sizeof(QByteArrayData))
            )
            static const qt_meta_stringdata_mywidget_t qt_meta_stringdata_mywidget = {
            {
            QT_MOC_LITERAL(0, 0, 8) // "mywidget"

            },
            "mywidget"
            

            };
            #undef QT_MOC_LITERAL

            static const uint qt_meta_data_mywidget[] = {

            // content:
            8, // revision
            0, // classname
            0, 0, // classinfo
            0, 0, // methods
            0, 0, // properties
            0, 0, // enums/sets
            0, 0, // constructors
            0, // flags
            0, // signalCount

               0        // eod
            

            };

            void mywidget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
            {
            Q_UNUSED(_o);
            Q_UNUSED(_id);
            Q_UNUSED(_c);
            Q_UNUSED(_a);
            }

            QT_INIT_METAOBJECT const QMetaObject mywidget::staticMetaObject = { {
            &QMainWindow::staticMetaObject,
            qt_meta_stringdata_mywidget.data,
            qt_meta_data_mywidget,
            qt_static_metacall,
            nullptr,
            nullptr
            } };

            const QMetaObject *mywidget::metaObject() const
            {
            return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
            }

            void *mywidget::qt_metacast(const char _clname)
            {
            if (!_clname) return nullptr;
            if (!strcmp(_clname, qt_meta_stringdata_mywidget.stringdata0))
            return static_cast<void
            >(this);
            return QMainWindow::qt_metacast(_clname);
            }

            int mywidget::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
            {
            _id = QMainWindow::qt_metacall(_c, _id, _a);
            return _id;
            }
            QT_WARNING_POP
            QT_END_MOC_NAMESPACE

            Thanks again for replying!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #12

              What is
              @Theok said in Running Qt Application from .dll:

              DLLSHARED_EXPORT

              ?

              Also, you seem to have a mix of header files which contain classes not having name matching the header file. Not that it is forbidden but it makes things unclear.

              What does your .pro file look like ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • T Offline
                T Offline
                Theok
                wrote on last edited by
                #13

                Thanks for reply,
                if i understand correct you need to define which classes,functions will be exported from the library so the user can use-call to his main app.

                #if defined(MYLIBRARY_LIBRARY)
                #  define MYLIBRARYSHARED_EXPORT Q_DECL_EXPORT
                #else
                #  define MYLIBRARYSHARED_EXPORT Q_DECL_IMPORT
                #endif
                

                This is the code of the xxx_myglobal.h header file that is automatically created when i create a C++ library project (The names changed with your recommendation so instead of MYLIBRARYSHARED_EXPORT before was DLLSHARED_EXPORT ). The .pro file of the shared library project is :

                #-------------------------------------------------
                #
                # Project created by QtCreator 2019-02-19T10:28:30
                #
                #-------------------------------------------------
                
                QT       += widgets
                
                TARGET = MyLibrary
                TEMPLATE = lib
                
                DEFINES += MYLIBRARY_LIBRARY
                
                # The following define makes your compiler emit warnings if you use
                # any feature of Qt which has been marked as deprecated (the exact warnings
                # depend on your compiler). Please consult the documentation of the
                # deprecated API in order to know how to port your code away from it.
                DEFINES += QT_DEPRECATED_WARNINGS
                
                # You can also make your code fail to compile if you use deprecated APIs.
                # In order to do so, uncomment the following line.
                # You can also select to disable deprecated APIs only up to a certain version of Qt.
                #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                
                SOURCES += \
                        mylibrary.cpp
                
                HEADERS += \
                        mylibrary.h \
                        mylibrary_global.h \ 
                    ui_windowlibrary.h
                
                unix {
                    target.path = /usr/lib
                    INSTALLS += target
                }
                
                

                Do you think the problem is on the C++ library project? This project builds fine but when i try to call the library from another application then comes the errors i wrote in the top...

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  The .pro file looks correct.

                  However, as suggested before, use consistent file naming. You have a widget called MainWindow, then you should have kept the original file name generated for you by Qt Creator. There's no need for any special naming because you are creating a library.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • T Offline
                    T Offline
                    Theok
                    wrote on last edited by
                    #15

                    After your recommendation i changed the names:
                    so the first code uploaded is not the same as the latest.
                    Named my library project mylibrary instead of dll and mainwindow class (Qt's default) -> mywidget class.

                    So my library builds correct,
                    But why i 'm having these errors when i try to build my app including my library?

                    #include "mylibrary.h"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        mywidget b;
                        b.show();
                        return a.exec();
                    }
                    
                    

                    This is the main.cpp file of my app. Just replacing with mywidget from the dll...

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      Can you show the .pro file of your application ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Theok
                        wrote on last edited by
                        #17

                        Finally i found another way to solve my problem.
                        @SGaist Thank you very much for your help and all the support. Keep going!
                        Best Regards,
                        Theo

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #18

                          What is it ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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