📜 ⬆️ ⬇️

Dynamic Libraries in Qt

Introduction


Before I started working with the Qt library, I programmed various tasks in C ++ at the university, and at work I used Delphi. And of course, in the process of work various libraries were created. Created, we can say with difficulty. What was lacking in the daily life of the advantages that the PLO produced. I always wanted to export entire classes from libraries, and to do it is simple, fast and carefree. At the same time, questions about creating libraries in C ++ and their further use in Delphi or C projects were very rare.

And here I am involved in the development of my first project using the Qt library. Reading books, assistant, forums, articles and ideas, ideas, ideas. The project is large, contains many components, and our team has gained experience, so to say, in combat conditions.

Creating a library


In the process, many tasks were set, one of which was the creation of a unified methodology for creating libraries for the needs of the project. That this technique could benefit novice programmers involved in the development. It is quite simple and experienced developers use it without thinking.

Most of the changes concern the * .pro file of your future library:
  1. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  2. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  3. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  4. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  5. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  6. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  7. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  8. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  9. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  10. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all
  11. # # , # pro- . TEMPLATE = lib DESTDIR = dist # . VERSION = 1 . 0 . 0 # TARGET = $$ qtLibraryTarget ( MyLibrary ) # , release debug CONFIG += build_all

About the qtLibraryTarget Function


The $$ qtLibraryTarget function complements the name of our library with the version number and the postfix of the build type (“d” for windows, “_debug” for mac, no linux postfix). That is, after assembling in the destination folder, we will see the files (windows):

Rule code


Now you need to configure the export of our code from libraries. To do this, create a new header file and name it, for example, MyLibrary_global.h. Its contents should be like this (Creator creates this file automatically when creating a dynamic library project):
  1. #ifndef MYLIBRARY_GLOBAL_H
  2. #define MYLIBRARY_GLOBAL_H
  3. #include <QtCore / qglobal.h>
  4. #if defined (MyLibrary_LIBRARY)
  5. # define MyLibrary_EXPORT Q_DECL_EXPORT
  6. #else
  7. # define MyLibrary_EXPORT Q_DECL_IMPORT
  8. #endif
  9. #endif / * MYLIBRARY_GLOBAL_H * /

Add the created file to the project:
  1. HEADERS + = MyLibrary_global . h
  2. # And we add a mandatory line with the export macro:
  3. DEFINES + = MyLibrary_LIBRARY

Exporting classes and functions


And now the most important thing. In all classes and functions that should be visible from the outside of the library, you need to add the previously defined macro MyLibrary_EXPORT (these files will be header files and must be supplied with the library binary files). For example, we want to export a class:
  1. #include "MyLibrary_global.h"
  2. class MyLibrary_EXPORT ComputerManager : public QObject {
  3. Q_OBJECT
  4. ...
  5. }
  6. // or function:
  7. MyLibrary_EXPORT QDebug operator << ( QDebug d , const MyObject & object ) ;

About building libraries in linux


As already mentioned, when building a library in linux, the postfix of the assembly type is not set (this can be seen by looking at the implementation of this qtLibraryTarget function in the qt source). But it does not matter, just enough to correct the lines in the pro-file:
  1. # If the operating system is from the unix family
  2. unix : {
  3. CONFIG ( debug , debug | release ) {
  4. # This name has a debug-version of the library.
  5. TARGET = ComputerManagerd
  6. } else {
  7. # And such a release version
  8. TARGET = ComputerManager
  9. }
  10. } else {
  11. TARGET = $$ qtLibraryTarget ( ComputerManager )
  12. }
  13. VERSION = 1 . 0 0
  14. # The first parameter is required to build # libraries in linux (qmake, make all),
  15. # second to build under the rest of the OS.
  16. CONFIG + = debug_and_release build_all
  17. # Specify folders for object files. For unix-like operating systems, this is critical.
  18. # If this is not done, then only the release version of the library will be collected,
  19. # or debug only. This is due to the fact that the files will replace each other.
  20. CONFIG ( debug , debug | release ) {
  21. OBJECTS_DIR = build / debug
  22. } else {
  23. OBJECTS_DIR = build / release
  24. }
This part of the pro-file will simultaneously create dynamic libraries in debug and release versions on windows, linux, mac.
')

Library use


In the project pro-file, you need to add the lines:
  1. # Connect the library header files
  2. INCLUDEPATH + = include / MyLibrary
  3. CONFIG ( debug , debug | release ) {
  4. # We connect debug-versions of libraries for different platforms
  5. win32 : LIBS + = - Llib - lMyLibraryd1
  6. unix : LIBS + = - Llib - L. - lMyLibraryd - Wl , - rpath , lib - Wl , - rpath,.
  7. } else {
  8. # We connect release-versions of libraries for different platforms
  9. win32 : LIBS + = - Llib - lMyLibrary1
  10. unix : LIBS + = - Llib - L. - lMyLibrary - Wl , - rpath , lib - Wl , - rpath,.
  11. }

This implies that in the root of your project there are two folders:
lib - Contains binary library files. include - contains the MyLibrary folder with all library header files.

Conclusion


The Qt library is a convenient tool for implementing your ideas and projects. It greatly simplifies the life of a programmer, because creating libraries in Qt projects is a very simple and understandable task.

Syntax Highlighting: Blog Editor from © SoftCoder.ru

Source: https://habr.com/ru/post/101994/


All Articles