Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Running Qt Application from .dll



  • Hi,
    i'm new to QT so maybe my question is silly,
    Is it possible to create a QT widget application with GUI, wrap it in a .dll library (along with the mainwindow and all the GUI stuff) and run it from another QT application (widget or console or whatever).
    My main goal is to not deploy an .exe but run the GUI from a .dll file.



  • Hi @Theok,
    In a Dynamic Library objects are defined, for example classes, functions, etc ...

    Have a look here: https://doc.qt.io/qt-5/sharedlibrary.html

    If you ship the header files of the objects from the dll, they can be easily used in other applications.

    If you use an opensource version of Qt, you have to link the Qt libraries dinamically. Which result that you need to ship these libraries as well.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Qt is a C++ framework so it follows the same rules. You are basically describing the modules from the Qt framework. They are built with or with graphics element.

    So yes, you can build your own set of libraries with custom widgets.



  • @Theok Well, what is the application you want to use as a host? How does it load shared libraries? It's certainly possible to have some sort of plugin architecture where code is loaded from a shared library, but you need to be a bit more specific about what you are doing. why is your goal to distribute a program but not distribute an exe? What is the actual problem you are trying tol solve?



  • @beecksche
    Thanks for replying, that's what I'm trying to do.Building a simple qt widget project with an empty window.After building a dynamic library project and copying the context of the header files and source files from the widget project to the dll project and building the dynamic library . At the end i am loading the library created (.dll,.lib and the header file of the dll) in a new project so to run the window from qt again. But i am having some warnings and errors and I don't know if my whole logic is wrong.



  • @wrosecrans
    Thanks for replying,
    I want to distribute my application in a dynamic library without exe because of some restrictions of the LGPL license . I haven't yet started building the app. I need to ensure that my whole up will be a dll to distribute and to run it again from qt ( I want my code to be closed source that's why dynamic linking ). My whole app will contain a simple gui window with tabs and other scientific plotting stuff.


  • Lifetime Qt Champion

    What exact errors are you getting ?



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



  • Forgot the includes in the dll.cpp:

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


  • Lifetime Qt Champion

    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.



  • @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!


  • Lifetime Qt Champion

    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 ?



  • 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...


  • Lifetime Qt Champion

    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.



  • 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...


  • Lifetime Qt Champion

    Can you show the .pro file of your application ?



  • 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


  • Lifetime Qt Champion

    What is it ?


Log in to reply