qt_add_plugin

创建 Qt 插件目标。

命令的定义在 核心 组件对于 Qt6 包,可以像这样加载:

find_package(Qt6 REQUIRED COMPONENTS Core)
					

该命令在 Qt 6.0 引入。

概要

qt_add_plugin(target
              [SHARED | STATIC]
              [CLASS_NAME class_name]
              [OUTPUT_TARGETS variable_name]
              [MANUAL_FINALIZATION]
              sources...
)
					

The MANUAL_FINALIZATION option and the ability to set sources were introduced in Qt 6.5.

无版本命令 被禁用,使用 qt6_add_plugin() 代替。它支持如此命令的一组相同自变量。

描述

Qt plugin targets have additional requirements over and above an ordinary CMake library target. The qt_add_plugin() command adds the necessary handling to ensure these requirements are met. It should be called rather than the built-in CMake add_library() command when defining a Qt plugin target.

By default, the plugin will be created as a STATIC library if Qt was built statically, or as a MODULE library otherwise. You can override this default by explicitly providing the STATIC or SHARED 选项。

任何 sources provided will be passed through to the internal call to add_library() .

注意: Non-static plugins are meant to be loaded dynamically at runtime, not linked to at build time. CMake differentiates between these two scenarios by providing the MODULE library type for dynamically loaded libraries, and the SHARED library type for libraries that may be linked to directly. This distinction is important for some toolchains (notably Visual Studio), due to the way symbol exports are handled. It may not be possible to link to MODULE libraries, and generating a SHARED library with no exported symbols can result in build-time errors. If the SHARED option is passed to qt_add_plugin() , it will therefore create a MODULE library rather than a SHARED 库。

Every Qt plugin has a class name. By default, this will be the same as the target , but it can be overridden with the CLASS_NAME option. The class name corresponds to the name of the C++ class that declares the metadata for the plugin. For static plugins, it is also the name passed to Q_IMPORT_PLUGIN , which imports the plugin into an application and ensures it is available at run time.

If the plugin is built statically, qt_add_plugin() may define additional internal targets. These facilitate automatic importing of the plugin for any executable or shared library that links to the plugin. If the project installs the plugin and intends to make it available for other projects to link to, the project should also install these internal targets. The names of these targets can be obtained by providing the OUTPUT_TARGETS option, followed by the name of a variable in which to return the target list.

定稿

After a target is created, further processing or finalization steps may be needed. The finalization processing is implemented by the qt_finalize_target() 命令。

For details and the meaning of the MANUAL_FINALIZATION option, refer to the finalization documentation for qt_add_library .

另请参阅 qt_finalize_target() and qt_add_executable() .