Some time ago said Qt Some of the use of the class library, today to change the taste, look at the program design problems. Today, it's about shared libraries.
If you open some Windows application directory, you will find that there are many programs exe files are very small, about hundreds of K look, and the directory is not only an EXE file, but also contains a lot of DLL files. These DLLs are actually some shared libraries, the so-called shared library, is actually some dynamic link library, can be loaded by the program at runtime Dynamic Library. Since it is shared, it means that these libraries are not only used by their own programs, but also can be used by other programs, such as some general-purpose algorithms. If you publish your own Qt program, you will also see a lot of shared libraries of the system, which are the QtGui.dll and the like. Perhaps you will say, I write the program does not share with other applications of the library, do not need these! actually otherwise Because one of the benefits of a shared library is that it can be loaded dynamically, that is, if you need to upgrade the program, you simply need to replace the DLL, you do not have to ask the user to reinstall all the files. Of course, these DLLs also have drawbacks: dynamically loaded things are certainly less efficient than statically compiled things. However, in today's hardware environment, this performance loss can be negligible.
What we're going to talk about today is how to create shared library code with QT.
We still use the qtcreator. When creating the project, we select the following C + + Library item and click OK.
In the next dialog, there is a drop-down list of shared libraries (share library), statically Linked library (static link libraries) and Qt 4 Plugin (qt 4 plugin). We select the first shared library, and the following steps will require you to choose which Qt modules to join, as before, select the part you want, and finally complete the project creation.
We'll see that Qtcreator has created some files for us. One of the {projectname}_global.h files was created for us by Qtcreator. Let's start with this {projectname}_global.h:
- #ifndef lib_global_h
- Span class= "preprocessor" > #define  LIB_GLOBAL_H
-  
- #include <QtCore/qglobal.h>
- &NBSP;
- #if defined (lib_library)
- # define libshared_export q_decl_export
- #else
- # define libshared_export q_decl_import
- #endif
- &NBSP;
- #endif // lib_global_h
This file only defines two macro libshared_export, note that LIB here is my project name. If the definition of lib_library,libshared_export is defined as Q_decl_export, it is defined as Q_decl_import. Look at this name, you know this is the object to export the statement. Let's write a window (if you want to do this, don't forget to tick the Qtgui module when creating the project, which is unchecked by default):
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, which 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 make it understood that I need to export this class MainWindow. The so-called export, is to compile it into a DLL file, the other classes can use this export class. Okay, here's the same thing, compile the project. In the Debug folder you get a Lib.dll file and a LIBLIB.A. The latter is a library used under Linux and is no longer detailed here.
Well, we're going to use this DLL. Create another project, you need it. Pro File Modification:
- 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 be able to find the two files of Lib.h and lib_global.h, you need to replace the path here with the one that matches your project. LIBS This line needs to tell the compiler (note that this is the compiler!) Where to find this DLL file. Then we write 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 used lib.h, but this file is not declared in HEADERS, and Qt is actually going to find this file from Includepath. MainWindow is not declared in the new test project, so where is it? Of course it's in the Lib.dll we compiled! We know that at the time of the link the compiler needs to find the implementation portal, that is, the DLL must be located, which is the place specified by this LIBS.
Finally compile and run this EXE file, how? Well, if you do as I say, you should get a mistake: you can't find Lib.dll. How could you not find it? Didn't you specify it with LIBS? Please note that we emphasize that this designation is the compile time. DLL is a dynamic link library, that is, you need to find this library when the EXE is running. The run-time lookup is in the following order: The current path, the system path (usually System32). So, to copy the Lib.dll we generated earlier into the EXE directory, and then directly double-click the exe file. A window came out! What's the difference? There is no difference in running, but we know that this window is implemented in this DLL! We want to add a button to the window? No problem, just add it! After the addition of a new DLL, copy to the Exe folder overwrite the old, the modification is complete! We don't need to modify this exe.
This time we recall that when we use the DLL we created, is it the same as using QtGui.dll? Only QtGui.dll has been placed in the library directory, do not need to manually modify the. Pro file add Includepath and LIBS.
This article is from the "Bean Space" blog, make sure to keep this source http://devbean.blog.51cto.com/448512/319479
QT Learning Pathway (60): Creating a shared library