Hello everyone, meet again, I am your friend Quanzhanjun.
The first two chapters have compiled the CTK plug-in library. In this article, we will write a plug-in to try it out. We need to create two small projects, one is the plug-in library and the other is the test program.
1. Writing plugin library
1.1 Create a project
Open Qt creator, create a new Emputy qmake Project, and name the project ctk-plugin-first. Kits select "Desktop Qt5.12.3 MSVC2017 64bit".

Change the ctk-plugin-first.pro file, add TARGET, CONFIG and other parameters, and add the header file path. It should be noted that two locations need to be added to the header file path, outside the source code directory, and the path to the generated files after compilation. Here, the MINGW compiler and the MSVC compiler are distinguished by the WIN64 macro definition.
copyQT += core QT -= gui TARGET = ctk-plugin-first TEMPLATE = lib CONFIG += plugin INCLUDEPATH += E:/lwks/CTK/Libs/Core \ += E:/lwks/CTK/Libs/PluginFramework if (contains(DEFINES,WIN64)){ # for msvc compiler INCLUDEPATH += E:/lwks/ctk-vsbuild/CTK-build/Libs/PluginFramework INCLUDEPATH += E:/lwks/ctk-vsbuild/CTK-build/Libs/Core }else{ # for mingw compiler INCLUDEPATH += E:/lwks/ctk-superbuild/CTK-build/Libs/PluginFramework INCLUDEPATH += E:/lwks/ctk-superbuild/CTK-build/Libs/Core }
1.2 Create a plugin class
Create a new class in the ctk-plugin-first project and name it FirstPluginActivator. The program source code is as follows. firstpluginactivator.h
copy#ifndef FIRSTPLUGINACTIVATOR_H #define FIRSTPLUGINACTIVATOR_H #include <QObject> #include "ctkPluginActivator.h" #include "ctkPluginContext.h" class FirstPluginActivator : public QObject, public ctkPluginActivator { Q_OBJECT Q_PLUGIN_METADATA(IID "FirstPlugin") Q_INTERFACES(ctkPluginActivator) public: FirstPluginActivator(); void start(ctkPluginContext *context); void stop(ctkPluginContext *context); }; #endif // FIRSTPLUGINACTIVATOR_H
firstpluginactivator.cpp
copy#include "firstpluginactivator.h" #include <QDebug> FirstPluginActivator::FirstPluginActivator() { } void FirstPluginActivator::start(ctkPluginContext *context) { qDebug() << "first plugin start: " << context; } void FirstPluginActivator::stop(ctkPluginContext *context) { qDebug() << "first plugin stop: " << context; }
The function of this class is relatively simple, only the start and stop services of the plug-in are implemented.
1.3 Create a resource file
Add a resource file to the project, the name can be arbitrarily chosen, I named it resource here. Add a prefix to the resource file, named /ctk-plugin-first/META-INF, pay attention to the naming of this prefix, the naming format is "/project name/META-INF", this cannot be changed at will. Create a new file under the prefix and name it MANIFEST.MF. The contents of the file are as follows.
copyPlugin-SymbolicName:FirstPlugin Plugin-Version:1.0.0
The MANIFEST.MF file is the manifest file of the plug-in library. You can set attributes such as the plug-in library name and version number in it, and you can also add some custom attributes. These attribute values can be read in the test program later. The entire plug-in library project is created, and the project file structure is shown in the figure below.

Compile the project, under normal circumstances, the ctk-plugin-first.dll file will be generated in the "build-ctk-plugin-first-Desktop_Qt_5_12_3_MSVC2017_64bit-Debug/debug" directory, which is the plug-in library file that needs to be loaded when the following test program runs .
2. Test program writing
2.1 Create a project
Open Qt Creator and create a new Qt Console Applciation project. The project name can be chosen arbitrarily. I set MainTest here, and kits also use Qt5.12.3 MSVC2017 64bit". For the newly created console application, the project file structure diagram should be as shown in the figure below, including .pro file and main.cpp file.

Modify the .pro project file and add the header file path of the CTK library and the path of the library file. Note that it is different from the .pro file configuration of the plug-in library project in that the configuration of LIBS is added, -L points to the output file path of the compiled CTK project, and specifies to link the CTKCore library and the CTKPluginFramework library.
copyQT -= gui CONFIG += console CONFIG -= app_bundle INCLUDEPATH += E:/lwks/CTK/Libs/Core \ += E:/lwks/CTK/Libs/PluginFramework if (contains(DEFINES,WIN64)){ # for msvc compiler INCLUDEPATH += E:/lwks/ctk-vsbuild/CTK-build/Libs/PluginFramework INCLUDEPATH += E:/lwks/ctk-vsbuild/CTK-build/Libs/Core LIBS += -LE:/lwks/ctk-vsbuild/CTK-build/bin/Debug -lCTKCore -lCTKPluginFramework }else{ # for mingw compiler INCLUDEPATH += E:/lwks/ctk-superbuild/CTK-build/Libs/PluginFramework INCLUDEPATH += E:/lwks/ctk-superbuild/CTK-build/Libs/Core LIBS += -LE:/lwks/ctk-superbuild/CTK-build/bin -lCTKCore -lCTKPluginFramework } DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
2.2 Write a test program
In main.cpp, first initialize the plug-in framework, then obtain the context of the plug-in service, use the plug-in context to install the plug-in, and start the plug-in, the code is as follows.
copy#include <QCoreApplication> #include <ctkPluginFrameworkFactory.h> #include <ctkPluginFramework.h> #include <ctkPluginException.h> #include <ctkPluginContext.h> #include <QtDebug> #include <QUrl> #ifdef __MINGW32__ // Definition when using the mingw compiler QString static firstPlugin_filePath = "E:/lwks/build-ctk-plugin-first-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/debug/ctk-plugin-first.dll"; #else // Definition when using msvc compiler QString static firstPlugin_filePath = "E:/lwks/build-ctk-plugin-first-Desktop_Qt_5_12_3_MSVC2017_64bit-Debug/debug/ctk-plugin-first.dll"; #endif int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); ctkPluginFrameworkFactory frameworkFactory; QSharedPointer<ctkPluginFramework> framework = frameworkFactory.getFramework(); // Initialize and start the plugin framework try { framework->init(); framework->start(); qDebug() << "CTK plugin framework start..."; } catch (const ctkPluginException &e) { qDebug() << "CTK plugin framework init err: " << e.what(); return -1; } // Get the contex t of the plugin service ctkPluginContext* pluginContext = framework->getPluginContext(); try { // Install the plugin QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(firstPlugin_filePath)); qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString()); // Start the plugin plugin->start(ctkPlugin::START_TRANSIENT); qDebug() << "Plugin start..."; } catch (const ctkPluginException &e) { qDebug() << QString("Failed install or run plugin: ") << e.what(); return -2; } return a.exec(); }
2.3 Running the test
Compile and run the MainTest project, and the results of the program are as follows. It can be seen that after the ctk-plugin-first plugin is loaded and started, the start function of the plugin is called and executed.

Publisher: full stack programmer, please indicate the source: https://javaforall.cn/144928.html Original link: https://javaforall.cn