Qt learning path (60): Create shared library

Source: Internet
Author: User
Tags export class

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:

 
 
  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  
  10. #endif  
  11.  
  12. #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

 
 
  1. #ifndef LIB_H  
  2. #define LIB_H  
  3.  
  4. #include <QMainWindow>  
  5.  
  6. #include "lib_global.h"  
  7.  
  8. class LIBSHARED_EXPORT MainWindow : public QMainWindow {  
  9. public:  
  10.     MainWindow(QWidget *parent = 0);  
  11. };  
  12.  
  13. #endif // LIB_H 

Lib. cpp

 
 
  1. #include "lib.h"  
  2.  
  3. MainWindow::MainWindow(QWidget *parent)  
  4.     : QMainWindow(parent)  
  5. {  
  6. }  

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:

 
 
  1. TARGET = test  
  2. TEMPLATE = app  
  3.  
  4. SOURCES += main.cpp  
  5.  
  6. INCLUDEPATH += ../  
  7.  
  8. 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:

 
 
  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 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

Related Article

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.