Evgenii Legotckoi
Evgenii LegotckoiOct. 9, 2018, 3:16 p.m.

Qt/C++ - Tutorial 083. Creating a dynamic library and connecting it to another project

The forum raised the question of how to create a dynamic library and correctly connect it to a third-party project. Periodically, such questions arise, so consider one option of creating a dynamic dll for Windows using the standard wizards in Qt Creator.

In this case, the option will not be considered when the project is divided into subprojects, which are compiled as libraries and then connected to the main project. Because it will be the dynamic internal libraries of the project. Let's create exactly the external library, which theoretically could be distributed in the form of binaries.

Create two projects:

  1. QuiLib - this will be an external dynamic library that will contain one dialog box. This dialog box will open in the main project.
  2. WithDynamicLibrary - the project that will be used to connect this dynamic library.

Step 1

Select the project creation in the Qt Creator menu and select the type of our project. This will be a C ++ library.

Step 2

Let's write down the name of the project and its location

Step 3

Select the kits for the assembly project.

There is a very important point here, and which beginners can forget. If you build a project using a compiler of a particular version, then you can use these libraries only in a project that will be built by the compiler of the same version.

I am compiling this library with the MSVC2017 compiler.

Step 4

Select the required modules. For our library, the basic functionality is enough.

Step 5

Give the name of the library class that will be used in it. The name in this case will be the same as the name of the library itself. But you can change. It does not matter.

Step 6

Do you use a version control system? So add a project under version control. If not, do nothing. And just complete the creation of the library.

Step 7

Let's look at the project files and modify them a bit.

Project structure

QuiLib.pro

In this file there is information that this is the library. Here in this line.

TEMPLATE = lib

Here is the complete code of the pro file.

#-------------------------------------------------
#
# Project created by QtCreator 2018-10-09T19:33:33
#
#-------------------------------------------------

QT       += widgets

TARGET = QuiLib
TEMPLATE = lib

DEFINES += QUILIB_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 += \
        QuiLib.cpp

HEADERS += \
        QuiLib.h \
        quilib_global.h 

unix {
    target.path = /usr/lib
    INSTALLS += target
}

quilib_global.h

Header for defining export defaults in the library. Classes that will be flagged by export defaults will be available for use outside the library.

#ifndef QUILIB_GLOBAL_H
#define QUILIB_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(QUILIB_LIBRARY)
#  define QUILIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define QUILIBSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // QUILIB_GLOBAL_H

QuiLib.h

Let's correct a little the header file of the dialog box, we really need a dialog, which means we need to inherit the class in this header file from QDialog.

#ifndef QUILIB_H
#define QUILIB_H

#include "quilib_global.h"

#include <QDialog>

class QUILIBSHARED_EXPORT QuiLib : public QDialog
{

public:
    explicit QuiLib(QWidget* parent = nullptr);
};

#endif // QUILIB_H

QuiLib.cpp

We will also write the implementation of the dialog box designer, so that it will tell us using QLabel that this is a dialog box from an external library.

#include "QuiLib.h"

#include <QLabel>
#include <QGridLayout>


QuiLib::QuiLib(QWidget* parent) : QDialog(parent)
{
    QGridLayout* gridLayout = new QGridLayout(this);
    setLayout(gridLayout);
    gridLayout->addWidget(new QLabel("Hello world from dynamic library", this));
}

Step 8

Let's compile the project in Debug and Release versions.

Here, for example, what will be in the release directory of the library assembly. Of those files, we only need the QuiLib.dll and QuiLib.lib files. In addition to these files, you will also need header files from the project itself, but more on that later.

Step 9

Create a project that will use this dynamic library. The creation process will be standard, through a wizard in Qt Creator. You will need to select the Qt Application.

Add project name and location

Step 10

Specify the assembly kit.

Шаг 11

Enter the name of the class of the main window of the application, and also indicate from which class to inherit. I chose QWidget.

Step 12

Again, the choice of a version control system and the completion of the project creation process.

Step 13

Project Structure

In the directory of this project, create a directory QuiLib , in which we place the directories debug , release , include .

These directories will contain the compiled QuiLib.dll and QuiLib.lib libraries, respectively, of the debug version and the release version. The include directory will contain the header files QuiLib.h and quilib_global.h.

That is, we presented the situation in which we gave someone a compiled library so that it could connect and use it.

Step 14

Add the library to the project using a wizard. Of course, you can manually register everything, but if you doubt your abilities, and this is true, otherwise you would not have read this article, then we will use a wizard.

Step 15

We know that the library is external

Step 16

And also, that we will use it only for Windows. Here it is configured that the Debug and Release versions are located in different directories without any prefixes of debugging libraries. I just did not set them up. It is enough to specify one of the .lib * libraries either in the debug or release directory. The path to another version will be added automatically. You also need to specify the directory in which the header files are located. Traditionally this include** the directory.

Step 17

Finish adding

Step 18

You need to write a method that will invoke a dialog box from an external library. But first, look at where the third-party connection strings were added, which we saw on the wizard page in step 17.

So now let's take a look at the pro file of our project that will use the dynamic library.

WithDynamicLibrary.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-10-09T19:45:20
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WithDynamicLibrary
TEMPLATE = app

# 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

CONFIG += c++11

SOURCES += \
        main.cpp \
        Widget.cpp

HEADERS += \
        Widget.h

FORMS += \
        Widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/QuiLib/release/ -lQuiLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/QuiLib/debug/ -lQuiLib

INCLUDEPATH += $$PWD/GuiLib/include
DEPENDPATH += $$PWD/GuiLib/include

These are the most recent lines in this file.

Widget.ui

Through a graphic designer, we will add a button to the main window, upon pressing of which a dialogue from the external library will be invoked.

Widget.h

Let's write a slot to handle the click of a button.

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void onPushButtonClicked(); // Slot for processing a click on the button

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

Widget.cpp

And now we will process click of the button and we will cause a dialog box from external library.

#include "Widget.h"
#include "ui_Widget.h"

#include <QPushButton>

// We connect the slot to the signal from the button
#include <QuiLib/include/QuiLib.h>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // We connect the slot to the signal from the button
    connect(ui->pushButton, &QPushButton::clicked, this, &Widget::onPushButtonClicked);
}

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

void Widget::onPushButtonClicked()
{
    // Вызываем диалоговое окно
    QuiLib libWidget(this);
    libWidget.exec();
}

Note that you need to call the dialog box using the exec () method so that the internal loop of the dialog starts, which will wait for events. Otherwise, the dialogue will immediately close, since the slot will work, and the dialogue in this case is created on the method stack and upon completion of the method, the dialogue is destroyed. And the exec () method will end only when the corresponding event occurs, which closes the dialog.

Conclusion

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

nayk1982
  • Oct. 9, 2018, 9:49 p.m.

Хороший урок, все подробно расписано. Такой вопрос: версия Qt для дин.библиотеки не обязательно должна совпадать с версией Qt проекта, который эту библиотеку использует?

Evgenii Legotckoi
  • Oct. 10, 2018, 2:59 a.m.

Спасибо ))

Из того, что я читал в документации, следует, что библиотеки Qt бинарно совместимы по минорным версиям. То есть, если проект работал с Qt 5.6, то можно поменять библиотеки на Qt 5.7 и по прежнему всё будет работать. На практике, конечно, не всегда всё гладко проходит. То есть по идее, если динамическая библиотека использует Qt 5.6, а подключили её в проект с Qt 5.7, то должно работать. Но опять же оговорюсь, на практике может выйти иначе, особенно, если динамическая библиотека использовала Qt 5.7, а подключили проект на Qt 5.6. Как минимум мождете оказаться, что в Qt 5.6 в каком-то классе отсутствуют некоторые методы.

То есть теоретически возможно, практически, как карта ляжет.



Docent
  • Oct. 10, 2018, 4:21 a.m.

Вот какой вопрос возник: для запуска программы вне Qt приходится тащить с ехе'шником кучу dll. А для использования созданной dll не придется ли тащить с собой всё те же Qt5Core.dll, Qt5Gui.dll, Qt5Widgets.dll...? Особенно если дальнейшее использование созданной dll планируется без участия Qt

Evgenii Legotckoi
  • Oct. 10, 2018, 4:25 a.m.

Погодите. Если речь идёт о библиотеке, которая использует Qt, от естественно, что ей понадобятся все те модули, от которых зависит бибилотека. Например в данном примере используются модули Qt5Core, Qt5Gui, Qt5Widgets, соответсвенно их тоже придётся тащить с собой. Если же вы создаёте библиотеку без участия Qt, то и модули Qt не будут нужны.

Вы не можете запланировать использование библиотеки без Qt, если она использует модули Qt, но если вы отказываетесь от использования Qt в библиотеке, то тогда получаете возможность не тащить все выше перечисленные модули, поскольку библиотека от них не зависит тогда.

o
  • Oct. 10, 2018, 4:28 a.m.

Круто было бы прочитать про приложение с подключаемыми плагинами.

Evgenii Legotckoi
  • Oct. 10, 2018, 4:31 a.m.

Типо как в Qt Creator?

Самому бы интересно было о таком почитать. В данный момент я бы мог написать только о написании плагинов для Qt Designer. С этим есть некоторый опыт.

o
  • Oct. 10, 2018, 5:46 a.m.

ну типа того, создание программы, функционал которой можно расширять плагинами, и, в перспективе, создание API.

o
  • Oct. 10, 2018, 5:48 a.m.

О плагинах к QtCreator в целом, тоже интересно.

Evgenii Legotckoi
  • Oct. 10, 2018, 5:50 a.m.

Если и начинать писать о плагинах, то нужно тогда с Qt Creator начинать, там наверняка будет одинаковый принцип, но по Qt Creator хотя бы информация есть.

o
  • Oct. 10, 2018, 9:16 a.m.

наверняка, так и есть)

B
  • Oct. 20, 2018, 12:51 p.m.

В принципе у меня есть опыт реализации плагинов, могу что-нибудь накропать как будет время

Evgenii Legotckoi
  • Oct. 21, 2018, 11:10 a.m.

Это было бы здорово и полезно ))

БГ
  • Jan. 8, 2019, 11:27 a.m.

При запуске приложения библиотека должна лежать рядом с исполняемым файлом. А как сделать так, чтобы библиотека лежала в папке на уровень ниже чем сам исполняемый файл?

nayk1982
  • Jan. 8, 2019, 9:02 p.m.

QApplication::addLibraryPath()

Можно в каталоге приложения создать файл qt.conf в котором прописать пути библиотек:

[Paths]
Libraries=./lib
Plugins=./lib

Edward Wayne
  • March 4, 2019, 2:17 a.m.

А можно динамическую библиотеку, скомпелированную в Visual Studio и никак не связанную с Qt, подключить в проект который разрабатывается в Qt?

Какие действия для этого нужно сделать?

Достаточно ли будет просто заменить эти строки:

#if defined(QUILIB_LIBRARY)
#  define QUILIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define QUILIBSHARED_EXPORT Q_DECL_IMPORT
#endif

на эти:

#define QUILIB_EXPORT __declspec(dllexport)

?

Evgenii Legotckoi
  • March 4, 2019, 4:22 a.m.

Полагаю, что да, нужно переписать экспорт, как вы написали. А подключение в Qt проекте будет аналогичным, такженаличие пути к библиотеке и заголовочные файлы. Главное, чтобы компиляторы были одной версии.

e
  • Nov. 16, 2020, 4:32 a.m.

здравствуйте! при компиляции библиотеки выскакивает окно особая программа( не удалось найти программу, укажите путь к ней), и в папке debug создается файл .dll, а .lib нет. подскажите, пожалуйста, в чем проблема.

DS
  • Feb. 16, 2022, 6:54 a.m.

Добрый день!
Очень мало информации, как писать классы с методами для компиляции в динамическую библиотеку.
Пример: в классе QuiLib дополнительно есть методы, которые могут вызываться, например

void setText(QString t) {
qlabel->setText(t);
}

то этот метод следует обьявлять как virtual, чтобы потом вызвать его где надо, верно?

Evgenii Legotckoi
  • Feb. 17, 2022, 3:51 a.m.

Нет, не верно. Модификатор virtual помечает метод класса как виртуальный, что позволяет переопределять методы при наследовании классов. К библиотекам вообще никакого отношения не имеет.

Обычно функции отдельно помечаются макросом типо QUILIBSHARED_EXPORT, но проще написать класс helper со статическими методами, ибо потом меньше проблем с линковкой и компиляцией.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
B

C++ - Test 002. Constants

  • Result:16points,
  • Rating points-10
B

C++ - Test 001. The first program and data types

  • Result:46points,
  • Rating points-6
FL

C++ - Test 006. Enumerations

  • Result:80points,
  • Rating points4
Last comments
k
kmssrFeb. 9, 2024, 5:43 a.m.
Qt Linux - Lesson 001. Autorun Qt application under Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
Qt WinAPI - Lesson 007. Working with ICMP Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
EVA
EVADec. 25, 2023, 9:30 p.m.
Boost - static linking in CMake project under Windows Ошибка LNK1104 часто возникает, когда компоновщик не может найти или открыть файл библиотеки. В вашем случае, это файл libboost_locale-vc142-mt-gd-x64-1_74.lib из библиотеки Boost для C+…
J
JonnyJoDec. 25, 2023, 7:38 p.m.
Boost - static linking in CMake project under Windows Сделал всё по-как у вас, но выдаёт ошибку [build] LINK : fatal error LNK1104: не удается открыть файл "libboost_locale-vc142-mt-gd-x64-1_74.lib" Хоть убей, не могу понять в чём дел…
G
GvozdikDec. 19, 2023, 8:01 a.m.
Qt/C++ - Lesson 056. Connecting the Boost library in Qt for MinGW and MSVC compilers Для решения твой проблемы добавь в файл .pro строчку "LIBS += -lws2_32" она решит проблему , лично мне помогло.
Now discuss on the forum
AC
Alexandru CodreanuJan. 19, 2024, 10:57 p.m.
QML Обнулить значения SpinBox Доброго времени суток, не могу разобраться с обнулением значение SpinBox находящего в делегате. import QtQuickimport QtQuick.ControlsWindow { width: 640 height: 480 visible: tr…
BlinCT
BlinCTDec. 27, 2023, 7:57 p.m.
Растягивать Image на парент по высоте Ну и само собою дял включения scrollbar надо чтобы был Flickable. Так что выходит как то так Flickable{ id: root anchors.fill: parent clip: true property url linkFile p…
Дмитрий
ДмитрийJan. 10, 2024, 3:18 p.m.
Qt Creator загружает всю оперативную память Проблема решена. Удалось разобраться с помощью утилиты strace. Запустил ее: strace ./qtcreator Начал выводиться весь лог работы креатора. В один момент он начал считывать фай…
Evgenii Legotckoi
Evgenii LegotckoiDec. 12, 2023, 5:48 p.m.
Побуквенное сравнение двух строк Добрый день. Там случайно не высылается этот сигнал textChanged ещё и при форматировани текста? Если решиать в лоб, то можно просто отключать сигнал/слотовое соединение внутри слота и …

Follow us in social networks