QT Learning Pathway (60): Creating a shared library

Source: Internet
Author: User
Tags export class

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:

    1. #ifndef  lib_global_h  
    2. Span class= "preprocessor" > #define  LIB_GLOBAL_H  
    3.  
    4. #include  <QtCore/qglobal.h>  
    5. &NBSP;
    6. #if  defined (lib_library)  
    7. #  define libshared_export q_decl_export  
    8. #else  
    9. #  define libshared_export q_decl_import  
    10. #endif  
    11. &NBSP;
    12. #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
    3. #include <QMainWindow>
    4. #include "lib_global.h"
    5. Class Libshared_export MainWindow: Public Qmainwindow {
    6. Public
    7. MainWindow (Qwidget *parent = 0);
    8. };
    9. #endif//Lib_h

Lib.cpp

    1. #include "Lib.h"
    2. Mainwindow::mainwindow (Qwidget *parent)
    3. : Qmainwindow (parent)
    4. {
    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. SOURCES + = Main.cpp
    4. Includepath + =. /
    5. 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:

    1. #include <QtGui/QApplication>
    2. #include "Lib.h"
    3. int main (int argc, char *argv[])
    4. {
    5. Qapplication A (argc, argv);
    6. MainWindow W;
    7. W.show ();
    8. return a.exec ();
    9. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.