Some time ago I talked about the use of some Qt class libraries. Today I want to change my taste and look at the program design issues. Today we are talking about shared library.
If you open a directory for some Windows applications, you will find that the exe files of many programs are very small and may look like several hundred kb, and there is not only one exe file in the directory, it also contains a large number of dll files. These dll are actually some shared libraries. The so-called shared libraries are actually some dynamic link libraries that can be dynamically loaded by programs during runtime. Since it is shared, that is to say, these libraries can be used not only by their own programs, but also by other programs, such as some common algorithms. If you publish your own Qt program, you will also see many system shared libraries, such as QtGui. dll. Maybe you will say that the program I wrote does not have a library shared with other applications, so you don't need this anymore! Actually not. One advantage of the shared library is that it can be dynamically loaded. That is to say, if you need to upgrade the program, simply replace the dll and you do not need to reinstall all files. Of course, these dll files also have disadvantages: dynamic loading is certainly less efficient than static compilation. However, in the current hardware environment, this performance loss is negligible.
Today we are talking about how to use Qt to create shared library code.
We still use QtCreator. When creating a project, select the following C ++ Library item and click OK.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1K63312U-0.png "alt =" "/>
In the following dialog box, there is a drop-down list, which is Shared Library), Statically Linked Library static Link Library), and Qt 4 PluginQt 4 plug-in ). Select the first shared library. In the subsequent steps, we will require you to select which Qt modules to add. In the same way as before, select the required parts and finally create the project.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1K6332046-1.png "alt =" "/>
We can see that QtCreator has helped us create some files. One of the {projectName} _ global. h files is created by QtCreator for us. Next we start with {projectName} _ global. h:
- #ifndef LIB_GLOBAL_H
- #define LIB_GLOBAL_H
-
- #include <QtCore/qglobal.h>
-
- #if defined(LIB_LIBRARY)
- # define LIBSHARED_EXPORT Q_DECL_EXPORT
- #else
- # define LIBSHARED_EXPORT Q_DECL_IMPORT
- #endif
-
- #endif // LIB_GLOBAL_H
This file only defines two macros, LIBSHARED_EXPORT. Note that the LIB here is my project name. If LIB_LIBRARY is defined, LIBSHARED_EXPORT is defined as Q_DECL_EXPORT; otherwise, it is defined as Q_DECL_IMPORT. By looking at this name, we can see that this is the statement for exporting the object. Next, let's write a window. If you want to do this, do not forget to check the QtGui module when creating the project. By default, it is not checked ):
Lib. h
- #ifndef LIB_H
- #define LIB_H
-
- #include <QMainWindow>
-
- #include "lib_global.h"
-
- class LIBSHARED_EXPORT MainWindow : public QMainWindow {
- public:
- MainWindow(QWidget *parent = 0);
- };
-
- #endif // LIB_H
Lib. cpp
- #include "lib.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- }
The code is simple, that is, to create a MainWindow. The only difference from the previous Code is that the LIBSHARED_EXPORT macro is used in the header file. You can simply understand it as, I need to export this class MainWindow. The so-called export is to compile it into a dll file, other classes can use this export class. Now, compile the project as before. In the debug folder, you get a lib. dll file and liblib.. The latter is the library used in Linux, which is not detailed here.
Now, we are going to use this dll. To create another project, modify the. pro file:
- TARGET = test
- TEMPLATE = app
-
- SOURCES += main.cpp
-
- INCLUDEPATH += ../
-
- LIBS += ../debug/lib.dll
First, we added the INCLUDEPATH line. This line is for our test project to find the lib. h and lib_global.h files. You need to replace the path here with the path that fits your project. The LIBS line should be noted to the compiler. Here is the compiler !) Find the dll file. Then we compile main. cpp:
- #include <QtGui/QApplication>
- #include "lib.h"
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
Note that we use lib. h, but this file is not declared in HEADERS. Qt actually finds this file from INCLUDEPATH. Where is MainWindow not declared in the new test project? Of course it is in the compiled lib. dll! We know that when linking, the compiler needs to find the implementation entry, that is, the dll must be located, which is the place specified by the LIBS.
Finally, compile and run this exe file. How can this problem be solved? Oh, if you did what I said, you should get an error: cannot find lib. dll. Why can't I find it? Didn't I use LIBS to specify it? Please note that we have emphasized that this is specified during the compilation period. Dll is a dynamic link library, that is, you need to find this library during the exe operation. The running search sequence is: current path-> system path (usually system32 ). Therefore, copy the previously generated lib. dll to the directory where the exe is located, and double-click the exe file. A window is coming out! What is the difference? There is no difference in running, but we know that this window is implemented in this dll! Do we want to add a button to the window? No problem. Add it! Recompile a new dll and copy it to the exe folder to overwrite the old one. The modification is complete! We do not need to modify this exe.
At this time, let's recall that when we use a self-created dll, is it the same as when we use QtGui. dll? However, QtGui. dll is already stored in the library directory, and you do not need to manually modify the. pro file to add INCLUDEPATH and LIBS.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/319479