Generation and call of QT dynamic link library so

Time:2020-3-31

QT creates and calls shared library file.so under Linux
After a little effort, I finally got the new skill of writing shared library and calling it!
The advantages of DLLs are self-evident. For a slightly more complicated program, good class library division at the top level of design can make the work very simple, while compiling different kinds of DLLs into shared library files can realize the modularization of the program, improve the degree of code reuse, and update a program only by updating shared Library (DLL in windows, DLL in windows, DLL in windows, DLL in windows, DLL in windows, DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL in DLL, So) is common under Linux.
Take the IDE QT Creator under Linux as an example to describe the compilation and invocation of QT (strictly speaking, C + +) shared library files.
Create shared library project
To create a new QT creator project, libraries – > C + + library, the type is shared library, all the way to next. When selecting modules, you can select them as required. By default, only qtcore module is selected (if you want to include UI, you must select qtgui). The shared library project I created is named plugintest.
Double click to open the plugintest? Global. H file of the project, and I add a line:

#include <iostream>Non essential

XXX ﹣ global. H is the header file of all library projects. The inclusion declaration of the QT module selected for use is also in this file.
Edit plugintest. H and plugintest.cpp as needed. This is the plugintest class of the shared library project. I added a helloword() public function, which contains:
STD:: cout < < Hello world! “< STD:: endl; / / / change to a simple summation function

Save and compile the project. If there is no problem, the libplugintest.so.1.0.0 shared library file, libplugintest.so, libplugintest.so.1 and libplugintest.so.1.0 soft links will be generated in the build plugintest desktop debug directory.
Project calling shared library
Modify the existing project pro file, or create a new common QT console project and modify its pro file. Add the following lines:

As mentioned in my previous QT article, includepicth added the directory of pluginttest project to find the header file, while LIBS added the shared library file, – L added the directory, – L specified the shared library name (note that the shared library project name is not the shared library file name). The previous failure was the fault of LIBS.
Modify main.cpp to call helloworld() of plugintest. Main.cpp is as follows:

#include <QCoreApplication>
#Include "plugintest. H" // include the plugintest. H header file
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Plugintest test; // plugintest is the class name defined in plugintest. H
    Test. Helloworld(); // call HelloWorld
    //return a.exec();
}

Save the compiled project and run it. You’ll get Hello world without accident! Modify the helloworld() function of the plugintest project to recompile the shared library, and run the main program to get different output results (without recompiling the main program), which is the benefit of the shared library (Dynamic Link Library).

Note: in Linux, the existence of soft connection is mainly to solve the version problem of dynamic library. In terms of the dynamic library generated by QT that I used, whether using libplugintest.so, libplugintest.so.1, libplugintest.so.1.0 or libplugintest. Any one of 1.0.0 can achieve the same effect.

Recommended Today

PHP Basics – String Array Operations

In our daily work, we often need to deal with some strings or arrays. Today, we have time to sort them out String operation <?php //String truncation $str = ‘Hello World!’ Substr ($STR, 0,5); // return ‘hello’ //Chinese string truncation $STR = ‘Hello, Shenzhen’; $result = mb_ Substr ($STR, 0,2); // Hello //First occurrence of […]