I. Generation of static libraries
1. Test Catalog: Lib
2. source file name: Mywindow.h, Mywindow.cpp, class Mywindow inherit from Qpushbutton, and set the text to "I ' m in class Mywindow";
3. Preparation of project documents: Mywindow.pro
Note two points:
TEMPLATE = Lib
CONFIG + = Staticlib
4. Generate Makefile:
Qmake
5. Compile and build the static library LIBMYWINDOW.A
Make
Two. Use of static libraries
1. Testing the Catalog: Test
2. Copy the Mywindow.h and LIBMYWINDOW.A to the test directory
3. Write the main.cpp, including the header file Mywindow.h, and call the Mywindow class
4. Preparation of project documents: Test.pro
Note Add the library path and the library file name:
LIBS + = L./-lmywindow
5. Generate Makefile:qmake
6. Compiling: Make
7. Run:./test
The generation and use of shared libraries in QT
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 helped us create some files. One of the {projectname}_global.h files was created for us by Qtcreator. Let's start with this {projectname}_global.h:
1 #ifndef lib_global_h
2 #define Lib_global_h
3
4 #include <QtCore/qglobal.h>
5
6 #if defined (lib_library)
7 # define Libshared_export Q_decl_export
8 #else
9 # define Libshared_export Q_decl_import
Ten #endif
11
#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
1 #ifndef lib_h
2 #define Lib_h
4 #include <QMainWindow>
5
6 #include "lib_global.h"
8 class Libshared_export Mainwindow:public Qmainwindow {
9 Public:
Ten MainWindow (qwidget *parent = 0);
One };
12
13
Lib.cpp
1 #include "lib.h"
3 Mainwindow::mainwindow (Qwidget *parent)
4:qmainwindow (parent)
5 {
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:
1 TARGET = Test
2 TEMPLATE = App
3
4 SOURCES + = Main.cpp
6 Includepath + =. /
7
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:
1 #include <QtGui/QApplication>
2 #include "lib.h"
3
4 int main (int argc, char *argv[])
5 {
6 qapplication A (argc, argv);
7 MainWindow W;
8 w.show ();
9 return a.exec ();
10}
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.
Generation and use of static libraries in QT