Creation and calling of Qt shared library (Dynamic Link Library) and static Link Library, and qt static Link Library
Qt Creator compiler, win7 64-bit System Environment
1. Create a shared library:
Create a file or project-> select Library and c ++ Library-> select shared Library-> next (Project name: sharedlib)
Generated directory structure:
Modify the code in sharedlib. h:
// Sharedlib. h # ifndef SHAREDLIB_H # define SHAREDLIB_H # include "sharedlib_global.h" class SHAREDLIBSHARED_EXPORT Sharedlib // export class. The client can directly load {public: Sharedlib (); void test ();}; extern "C" Q_DECL_EXPORT int add (int a, int B); // export function, which can be loaded by QLibrary on the Client # endif // SHAREDLIB_H
Modify the code in sharedlib. cpp:
// sharedlib.cpp#include "sharedlib.h"#include <QMessageBox>Sharedlib::Sharedlib(){}void Sharedlib::test(){ QMessageBox::warning(0, "Sharedlib::test", "Sharedlib::test");}int add(int a, int b){ return a+b;}
2. Call the Shared Library (method 1 ):
Create a file or project-> select Application, QtConsoleApplication-> project name app1-> next
Modify the code in main. cpp of the client program:
// Main. cpp # include <QCoreApplication> # include <QLibrary> # include <iostream> using namespace std; int main (int argc, char * argv []) {QCoreApplication a (argc, argv); typedef int (* Add) (int a, int B); QLibrary mylib ("sharedlib. dll "); if (! Mylib. load () {// failed to load dll cout <"failed to load sharedlib. dll! "<Endl; return-1;} Add add = (Add) mylib. resolve ("add"); if (0 = add) {// failed cout loading <"failed to load function add! "<Endl; return-1;} int sum = add (1, 2); // call the add function cout <sum <endl; return 0; return a.exe c ();}
Compile the sharedlib project and app1 project-> copy the generated sharedlib. dll file to the directory where app1.exe is located-> RUN app1.exe to output 3
Conclusion: This call method must be provided during client program compilation. the dll name and function information must be provided when the client is running. dll file, the client has a lot of code to write.
3. Call the Shared Library (method 2 ):
Create a file or project-> select Application, QtQWidgetsApplication-> project name app2-> next
Add the following code at the end of the app2.pro file to describe the path information of sharedlib. lib:
unix|win32: LIBS += -L$$PWD/../build-sharedlib-unknown-Debug/debug/ -lsharedlibINCLUDEPATH += $$PWD/../build-sharedlib-unknown-Debug/debugDEPENDPATH += $$PWD/../build-sharedlib-unknown-Debug/debug
Copy the sharedlib. h and sharedlib_global.h header files under the source code directory of the sharedlib project to the app2 project source code directory, and copy the sharedlib. dll file to the directory where app2.exe is located.
Modify the code in main. cpp of the client program:
// main.cpp#include "mainwindow.h"#include <QApplication>#include "sharedlib.h"int main(int argc, char *argv[]){ QApplication a(argc, argv);// MainWindow w;// w.show(); Sharedlib sb; sb.test(); return a.exec();}
Compile the app2 project and run app2.exe. A dialog box is displayed, indicating that the program is correctly executed:
Conclusion: The. lib and. h files must be provided during client program compilation. The. dll files must be provided when the client program is running. The client has less code to write.
4. Similarities and Differences between method 1 and Method 2:
The client only needs to run the program. dll files; there are some differences during compilation. There are many methods for one code, but no need. lib and. h file. Method 2: the code is simple (the same as calling a common class), but it is required. lib and. h file.
The file structure after three projects are created:
5. Create a static Link Library:
Create a file or project-> select Library and c ++ Library-> select static Link Library-> next (Project name: staticdlib)
Modify the code in staticlib. h:
// staticlib.h#ifndef STATICLIB_H#define STATICLIB_Hclass Staticlib{public: Staticlib(); void test();};#endif // STATICLIB_H
Modify the code in staticlib. cpp:
// staticlib.cpp#include "staticlib.h"#include <iostream>using namespace std;Staticlib::Staticlib(){ cout<<"Staticlib::Staticlib"<<endl;}void Staticlib::test(){ cout<<"Staticlib::test"<<endl;}
After the project is compiled, the staticlib. lib file is generated.
6. Call the static Link Library:
Create a file or project-> select Application, Qt Console Application-> project name app-> next
Add the following code at the end of the app. pro file to describe the path information of staticlib. lib:
unix|win32: LIBS += -L$$PWD/../build-staticlib-unknown-Debug/debug/ -lstaticlibINCLUDEPATH += $$PWD/../build-staticlib-unknown-Debug/debugDEPENDPATH += $$PWD/../build-staticlib-unknown-Debug/debug
Copy staticlib. h under the staticlib project directory to the app project directory.
Modify the code in main. cpp of the client program:
// main.cpp#include <QCoreApplication>#include "staticlib.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Staticlib sb; sb.test(); return a.exec();}
Compile the app project, generate the app.exe file, run the app.exe file, and output:
Conclusion: The. h and. lib files are required for client program compilation, and no other files are required during runtime.
7. Similarities and Differences between a shared library (Dynamic Link Library) and a static Link Library:
Exception: The. dll file is required when the client that calls the shared library is running, and the static link library is not required;
When the library program changes, the shared library client only needs to update the. dll file, and the static Link Library client needs to be re-compiled.
Same: it can be used for multi-module development.
Note: This article is the original article, welcome to consult or propose amendments, contact QQ: 1871046323@qq.com
Source Code address:<A href ="Https://github.com/ZhangShuaiH/Qt-demos/tree/master">Https://github.com/ZhangShuaiH/Qt-demos/tree/master</A>